网页入场动画之 Animate

话不多说看效果

提示: gif不动了就刷新下页面 !!!!!!!!!!!!!!!!

在这里插入图片描述
仔细看 Animate 就是动态添加一些它自己封装的一些样式类 这就是用法
但是类前面必须加 animate__animated
在这里插入图片描述

1、安装 animate

npm install animate.css --save

2、main 引入

import animated from 'animate.css' // npm install animate.css --save安装,再引入
Vue.use(animated)

下面就是一些动态添加样式的方法

Animate 还有很多的动画 这只是冰山一角 具体可以去 去官网看
这里我加了鼠标滚动监听,上滚删类 下滚添加类

// 这是一个vue的组件  可直接拿走  引入的组件是下一段代码
<template>
  <div class="hello">
    <h1 :class="css1">帅帅1</h1>
    <h2 :class="css2">帅帅2</h2>
    <h3 :class="css3">帅帅3</h3>
    <h4 :class="css4">帅帅4</h4>
    <Hell2></Hell2>
  </div>
</template>

<script>
import Hell2 from "./hell2.vue";
export default {
  name: "HelloWorld",
  components: {
    Hell2
  },
  data() {
    return {
      css1: "",
      css2: "",
      css3: "",
      css4: ""
    };
  },
  created() {
    this.cssa();
    this.cssb();
    this.cssc();
    this.cssd();
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll, true); // 监听(绑定)滚轮 滚动事件
  },
  methods: {
    css() {
      this.css1 = "";
      this.css2 = "";
      this.css3 = "";
      this.css4 = "";
    },
    cssa() {
      this.css1 = "animate__animated animate__fadeInLeft ";
    },
    cssb() {
      this.css2 = "animate__animated animate__fadeInRight";
    },
    cssc() {
      this.css3 = "animate__animated animate__bounceInDown animate__delay-1s";
    },
    cssd() {
      this.css4 = "animate__animated animate__bounceInUp animate__delay-1s";
    },
    handleScroll() {
      // 监听鼠标滚动
      // 页面滚动距顶部距离
      var scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      var scroll = scrollTop - this.i;
      this.i = scrollTop;
      if (scroll < 0) {
        // 鼠标上滚
        this.css();
      } else {
        // 鼠标下滚
        this.cssa();
        this.cssb();
        this.cssc();
        this.cssd();
      }
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.hello {
  // height: 1500px;
  display: flex;
  flex-wrap: wrap;
  background-size: c;
  background: url(../assets/bg.png) no-repeat;
  h1 {
    width: 50%;
    margin-top: 300px;
    text-align: center;
    height: 200px;
    line-height: 200px;
    background: #333;
  }
  h2 {
    margin-top: 300px;
    width: 50%;
    text-align: center;
    height: 200px;
    line-height: 200px;
    background: #444;
  }
  h3 {
    width: 50%;
    text-align: center;
    height: 200px;
    line-height: 200px;
    background: #555;
    margin: 0;
  }
  h4 {
    width: 50%;
    text-align: center;
    height: 200px;
    line-height: 200px;
    background: #666;
    margin: 0;
  }
}
</style>

这里面有组件进入可视区域监听

// 这是上面引入的那个组件  改吧改吧就是你想要的效果
<template>
  <div class="hello">
    <h5 :class="css5">帅帅5</h5>
    <h6 :class="css6">帅帅6</h6>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      css5: "",
      css6: ""
    };
  },
  created() {},
  mounted() {
    window.addEventListener("scroll", this.scrollHandle, true); // 监听 监听元素是否进入/移出可视区域
  },
  methods: {
    csse() {
      this.css5 =
        "animate__animated animate__fadeInTopLeft animate__delay-0.5s animate__slow-5s";
      // window.removeEventListener('scroll', this.scrollHandle, true); // 记得在适当的时候移除事件监听
    },
    cssf() {
      this.css6 =
        "animate__animated animate__fadeInRightBig animate__delay-0.5s animate__slow-5s";
      // window.removeEventListener('scroll', this.scrollHandle, true); // 记得在适当的时候移除事件监听
    },
    scrollHandle() {
      const offset = this.$el.getBoundingClientRect();
      const offsetTop = offset.top;
      const offsetBottom = offset.bottom;
      // const offsetHeight = offset.height;
      // 进入可视区域
      // console.log(offsetTop,offsetBottom)
      if (offsetTop <= window.innerHeight && offsetBottom >= 0) {
        // console.log('进入可视区域');
        // do something...
        this.csse();
        this.cssf();
      } else {
        // console.log('移出可视区域');
        // this.enabledPause = false;
        // do something...
        this.css5 = "";
        this.css6 = "";
      }
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.hello {
  height: 1500px;
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  margin-top: 500px;
  overflow: hidden;
  h5 {
    width: 50%;
    text-align: center;
    height: 200px;
    line-height: 200px;
    background: #777;
    margin: 0;
  }
  h6 {
    width: 50%;
    text-align: center;
    height: 200px;
    line-height: 200px;
    background: #888;
    margin: 0;
  }
}
</style>

-----------------------------------------------你在变强 也在变亮--------------------------------------------------

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在Vue中使用CSS动画animate可以通过以下步骤实现: 1. 在Vue组件中引入animate.css动画库 ``` import 'animate.css'; ``` 2. 在需要使用动画的元素上添加animate.css中的动画类名 ``` <template> <div class="box animate__animated animate__bounce">Hello World!</div> </template> ``` 3. 可以通过Vue的transition组件来实现进入和离开动画 ``` <template> <transition name="fade"> <div v-if="show" class="box">Hello World!</div> </transition> </template> <style> .fade-enter-active, .fade-leave-active { transition: opacity .5s; } .fade-enter, .fade-leave-to { opacity: 0; } </style> ``` 以上就是在Vue中使用CSS动画animate的基本步骤。 ### 回答2: Vue.js是一款流行的前端JavaScript库,它可以帮助您快速构建动态响应的现代Web应用程序。其中之一功能强大的功能是使用CSS动画来为Vue组件添加特殊的视觉效果。在Vue中使用CSS动画通常需要使用类库来帮助管理动画animate.css是其中一个很好的选择。 Animate.css是一个轻量级的CSS动画框架,它提供了一个广泛的动画效果选项。它适用于各种类型的Web项目,包括Vue应用程序。下面是在Vue中使用Animate.css动画的步骤: 步骤1:安装Animate.css 首先,在Vue项目中安装Animate.css类库。可以使用npm包管理器来安装它: npm install animate.css --save 步骤2:导入Animate.css 在需要使用Animate.css动画的组件中,将该类库导入并添加样式类名称。可以通过在组件内部或与其相关的样式表中导入它。例如: <template> <div class="box"></div> </template> <script> import 'animate.css/animate.min.css'; export default { name: 'AnimateBox', data() { return { isVisible: true } } } </script> <style lang="scss"> .box { width: 100px; height: 100px; background-color: red; animation: bounce 2s infinite; } </style> 步骤3:添加动画 在组件的样式中,添加动画名称(例如bounce)和持续时间(例如2秒)。 通过向组件的数据属性添加isVisible变量来启动和停止动画。例如: <template> <div :class="{ 'animated bounce': isVisible }"></div> <button @click="isVisible = !isVisible">Toggle animation</button> </template> <script> import 'animate.css/animate.min.css'; export default { name: 'AnimateBox', data() { return { isVisible: true } } } </script> 请注意,类animated和bounce是Animate.css的两个类名。isVisible变量在组件内部初始化为true,这将启动动画。当单击按钮时,可以使用对象绑定切换isVisible属性,以停止或重新启动动画。 结束语: 在Vue中使用Animate.css动画是一种有趣且易于使用的方法,可以为您的Vue组件添加特殊的视觉效果,从而提高用户体验。上述是在Vue中使用Animate.css动画的基本步骤,对于想要在Vue中使用动画的开发者可以进行尝试。 ### 回答3: Vue中使用CSS动画animate可以为网页增加更加生动的视觉效果,提升用户体验。实际上,Vue已经默认支持了大多数的CSS过渡和动画效果,只需要在动态绑定的数据中添加过渡名称,然后在样式表中定义过渡规则即可。 首先,在Vue的数据绑定中使用transition和animation类名来定义需要添加动画效果的元素,这些类名可以与CSS过渡或动画样式表配合使用。针对不同过渡效果和事件,Vue通过以下类名提供对应的支持: 1. v-enter, v-leave-to: 在元素插入/移除之前使用,可定义过渡效果。 2. v-enter-active, v-leave-active: 在元素插入/移除之后使用,可定义过渡效果。 为了执行动画,Vue使用了Web Animations API。通过配置动画属性,开发者可以将CSS动画应用到Vue元素中。Vue实现动画的方式主要有以下两种: 1. 在CSS中设置关键帧(@keyframes)和动画名称,然后在Vue元素中使用类名来触发动画效果。 2. 使用Vue提供的<transition>和<animation>组件,在元素出现或消失时自动添加/删除指定类名。 最后,需要注意的是,由于动画效果需要占用大量的系统资源,因此在使用Vue中的CSS动画animate时,需要考虑优化动画效果,让其更加有效率。可以通过在CSS中增加属性,如will-change或translate3d,来优化动画效果。确保过渡和动画效果的各种属性都能够顺畅地运行才能让你的网站更加生动有趣。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值