提问:
1、hanzi-write生成的svg只能使用px像素大小,使用rem无效。2、子组件无法修改父组件背景色。
svg问题:
- <template>
- <div class="header">
- <div v-for="item of 200" :key="item" class="snowflake"></div>
- <div class="title">
- <div class="title__parent">
- <Transition
- name="fade"
- enter-active-class="animate__animated animate__slideInUp"
- >
- <p v-if="show" class="title__top">驶出孤岛</p>
- </Transition>
- </div>
- <div class="title__row">
- <div class="title__parent">
- <Transition
- name="fade"
- enter-active-class="animate__animated animate__slideInUp animate__fast delay"
- >
- <p v-if="show" class="title__row__left">的</p>
- </Transition>
- </div>
- <div class="title__row__right">
- <div id="character-target-div"></div>
- </div>
- </div>
- </div>
- </div>
- </template>
- <script>
- import { onMounted, ref } from "vue";
- import HanziWriter from "hanzi-writer";
- export default {
- name: "Header",
- setup() {
- const show = ref(false);
- const chainAnimations = () => {
- //创建笔法“淘”字实例
- var writeOne = HanziWriter.create("character-target-div", "淘", {
- width: 150, //宽
- height: 150, //高
- padding: 5, //内边距
- showCharacter: false, //不显示汉字
- showOutline: false, //开启动画时不显示汉字
- strokeColor: "#000000", //字体颜色
- strokeAnimationSpeed: 5, //动画速度
- delayBetweenStrokes: 10, //动画每笔间隔时间,默认1000
- });
- //创建笔法“金”字实例
- var writeTwo = HanziWriter.create("character-target-div", "金", {
- width: 150, //宽
- height: 150, //高
- padding: 5, //内边距
- showCharacter: false, //不显示汉字
- showOutline: false, //开启动画时不显示汉字
- strokeColor: "#000000", //字体颜色
- strokeAnimationSpeed: 5, //动画速度
- delayBetweenStrokes: 10, //动画每笔间隔时间,默认1000
- drawingWidth: 10,
- });
- //创建笔法“者”字实例
- var writeThree = HanziWriter.create("character-target-div", "者", {
- width: 150, //宽
- height: 150, //高
- padding: 5, //内边距
- showCharacter: false, //不显示汉字
- showOutline: false, //开启动画时不显示汉字
- strokeColor: "#000000", //字体颜色
- strokeAnimationSpeed: 5, //动画速度
- delayBetweenStrokes: 10, //动画每笔间隔时间,默认1000
- });
- setTimeout(function () {
- writeOne.animateCharacter({
- onComplete: function () {
- setTimeout(function () {
- writeTwo.animateCharacter({
- onComplete: function () {
- setTimeout(function () {
- writeThree.animateCharacter();
- }, 0);
- },
- });
- }, 0);
- },
- });
- }, 980);
- };
- //挂载执行
- onMounted(() => {
- show.value = true;
- chainAnimations();
- });
- return { show };
- },
- };
- </script>
- <style lang="scss" scoped>
- * {
- margin: 0;
- box-sizing: border-box;
- }
- #character-target-div {
- height: 100%;
- }
- #character-target-div svg {
- width: 1.6rem;
- }
- .header {
- width: 100%;
- height: 150vh;
- display: flex;
- justify-content: center;
- background: linear-gradient(
- to top,
- #fffbf7 40%,
- #fff5f5 50%,
- rgb(255, 218, 218) 99%
- ) !important;
- .title {
- width: 90%;
- height: 3.56rem;
- margin: 2.8rem 0 0 0;
- letter-spacing: 0.5rem;
- &__parent {
- display: block;
- overflow: hidden;
- }
- &__top {
- display: block;
- font-size: 1.496rem;
- font-family: 宋体;
- font-weight: bold;
- margin-left: 1rem;
- }
- &__row {
- display: flex;
- justify-content: flex-end;
- &__left {
- color: #000000;
- font-size: 1.496rem;
- font-family: 宋体;
- padding-right: 0.2rem;
- }
- &__right {
- color: #000;
- height: 1.5rem;
- font-size: 1.5rem;
- font-family: "Lucida Calligraphy", cursive, serif, sans-serif;
- padding-right: 0.5rem;
- }
- }
- }
- }
- .snowflake {
- --size: 1vw;
- width: var(--size);
- height: var(--size);
- background: url(../../images/falling.gif) no-repeat; //金色叶子
- background-size: 100% 100%;
- position: fixed;
- top: -10vh; //出事高度在屏幕外 效果更真实
- z-index: 1; //背景图层不遮挡上面元素
- }
- @keyframes fall {
- 100% {
- transform: translate3d(var(--end), 100vh, 0);
- }
- }
- @for $i from 0 through 200 {
- .snowflake:nth-child(#{$i}) {
- //每个雪花的大小
- --size: #{random(6) * 0.1}vw;
- //雪花移动目标点
- --end: #{random(20) - 40}vw;
- //雪花初始位置
- left: #{random(150)}vw;
- //雪花从顶到底移动的动画 动画时间可以调整雪花速度
- animation: fall #{5 + random(8)}s linear infinite;
- animation-delay: -#{random(10)}s;
- }
- }
- .animate__slideInUp {
- animation-duration: 0.9s;
- }
- .delay {
- animation-duration: .98s;
- animation-delay: 350ms;
- }
- </style>
父组件:
- <template>
- <div
- id="js-main-bg"
- class="c-main-bg"
- >
- <Footer />
- </div>
- </template>
- <script>
- import Footer from "../footer/Footer";
- export default {
- name: "ContentMain",
- components: {
- Footer,
- },
- setup() {
- const mainBg = document.getElementById("js-main-bg");
- return {
- mainBg
- }
- },
- };
- </script>
- <style lang="scss" scoped>
- .c-main-bg {
- height: 450vh;
- background-color: #f2f2f2;
- padding: 1.2rem 1rem 0.4rem;
- </style>
子组件:
- <template>
- <div class="section">
- <div class="section__row">
- <div class="section__row__left">
- <img
- src="../../images/pc1.jpg"
- alt=""
- />
- </div>
- <div class="section__row__right">
- <img
- src="../../images/pc2.jpg"
- alt=""
- />
- </div>
- </div>
- </div>
- </template>
- <script>
- import { mainBg } from "../contentMain/ContentMain";
- export default {
- name: "Footer",
- };
- </script>
- <style lang="scss" scoped>
- .section {
- display: flex;
- align-items: center;
- height: 200vh;
- &__row {
- display: flex;
- justify-content: space-between;
- align-items: flex-end;
- &__left {
- width: 55%;
- display: flex;
- justify-content: flex-end;
- }
- &__right {
- width: 32%;
- transform: translate(-20%);
- display: flex;
- justify-content: flex-end;
- }
- }
- img {
- width: 100%;
- border-radius: 0.5rem;
- }
- img:hover {
- filter: saturate(0);
- transition: .6s cubic-bezier(0,.89,.41,1);
- }
- }
- </style>
我想要达到的效果。1、修改hanzi-write生成的svg字体大小样式为rem。2、鼠标移入子组件的两个图片,父组件的背景色变为其他颜色,移出后还原。
解答:
问题1:
可以在使用 hanzi-write 生成的 SVG 字体的父元素上设置 font-size 属性,并使用 CSS 的 rem 单位:
可以通过修改父元素的 font-size 来控制生成的 SVG 字体的大小,而不会影响其他的字体大小。
- <template>
- <div>
- <hanzi-write
- :hanzi="hanzi"
- :width="width"
- :height="height"
- />
- </div>
- </template>
- <script>
- import HanziWrite from 'hanzi-write';
- export default {
- components: {
- HanziWrite
- },
- data() {
- return {
- hanzi: '你',
- width: 100,
- height: 100
- };
- }
- };
- </script>
- <style>
- div {
- font-size: 2rem;
- }
- </style>
、、、、、、、、、、、、、、、
问题2:
子组件
- <template>
- <img
- @mouseenter="changeBgColor('#ff0000')"
- @mouseleave="changeBgColor('#ffffff')"
- src="your-img-src"
- alt="your-img-alt">
- </template>
- <script>
- export default {
- methods: {
- changeBgColor(color) {
- this.$emit('change-bg-color', color)
- }
- }
- }
- </script>
父组件:
- <template>
- <div :style="{ backgroundColor: bgColor }">
- <your-component @change-bg-color="changeBgColor"></your-component>
- </div>
- </template>
- <script>
- import YourComponent from 'path-to-your-component'
- export default {
- components: {
- YourComponent
- },
- data() {
- return {
- bgColor: '#ffffff'
- }
- },
- methods: {
- changeBgColor(color) {
- this.bgColor = color
- }
- }
- }
- </script>