jQuery动画知识点总结;动画效果的导航

Original 2019-01-22 22:04:29 258
abstract:1、jQuery动画知识点总结<!DOCTYPE html><html><head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0&q

1、jQuery动画知识点总结

<!DOCTYPE html>

<html>


<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <script type="text/javascript" src="jquery-3.3.1.js"></script>

  <title>jQuery的动画效果——自定义动画</title>

  <style type="text/css">

    div {

      width: 200px;

      height: 200px;

      background: blue;

      position: absolute;

      color:#fff;

    }

  </style>

  <script type="text/javascript">

    $(document).ready(function() {

      $('.but1').click(function() {

        $('p').animate({

          fontSize: '40px'

        }, 1500) //属性名称font-size一律改成驼峰写法:fontsize

      })

      //同时操作多个css属性

      //使用预定义的值:show hide toggle

      $('.but2').click(function() {

        $('div').animate({

          // left: '200px',//如果想对元素位置进行一个位移,那么我们需要给当前元素设置一个position属性,例如:position: absolute;

          // opacity:'0.3',

          // height:'400px',

          // width:'400px'

          width:'toggle'

        }, 1500) //属性名称font-size一律改成驼峰写法:fontsize

      })



      $('#right').click(function(){

        $('.box1').animate({left:'+500px'},3000)

        $('.box1').animate({fontSize:'50px'},3000)

      })

      $('#stop').click(function(){

        $('.box1').stop(true,true)

      })

    })

  </script>

</head>


<body>

  <!-- jQuery中我们使用animate()方法创建自定义的动画

  语法:$(selector).animate({param},speed,fn)

  必需的params参数定义形成动画的CSS属性

  可选的speed参数规定效果的时长,它可以取以下值:"slow","fast",或毫秒

  可选的fn 是动画完成后所执行的函数


  stop()方法用于同志动画或效果,在它们完成之前,该方法适用于所有jQuery效果函数,包括滑动淡入淡出和自定义动画

  语法:$(selector).stop(stopAll,goToEnd)

  可选的参数stopAll规定是否应该清除动画队列,默认是false仅停止活动的动画,允许任何排入队列的动画向后执行

  可选的参数goToEnd规定是否立即完成当前动画。默认是false

  默认情况下stop()会清除在被选元素上指定的当前动画 -->

  <!-- <button>点击字体放大</button>

  <p>jQuery中我们使用animate()方法创建自定义的动画</p>


  <button>点击移动div</button>

  <div></div> -->

  点击右移按钮,div右移,点击停止按钮当前效果停止

  <br>

  <button id="right">右移</button>

  <button id="stop">停止</button>

  <div>php中文网</div>


</body>


</html>

捕获.PNG



2、动画效果的导航(jQuery下划线动画导航特效)

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>jQuery下划线动画导航特效</title>


<script type="text/javascript" src="jquery-3.3.1.js"></script>


<style>

* {

  box-sizing: border-box;

}


body {

  background: #1A1E23;

  font-family: "Lato", sans-serif;

  -webkit-font-smoothing: antialiased;

}


nav {

  position: relative;

  padding-bottom: 12px;

}

nav .line {

  height: 2px;

  position: absolute;

  bottom: 0;

  margin: 10px 0 0 0;

  background: #FF1847;

}

nav ul {

  padding: 0;

  margin: 0;

  list-style: none;

  display: flex;

}

nav ul li {

  margin: 0 40px 0 0;

  opacity: 0.4;

  transition: all 0.4s ease;

}

nav ul li:hover {

  opacity: 0.7;

}

nav ul li.active {

  opacity: 1;

}

nav ul li:last-child {

  margin-right: 0;

}

nav ul li a {

  text-decoration: none;

  color: #fff;

  text-transform: uppercase;

  display: block;

  font-weight: 600;

  letter-spacing: 0.2em;

  font-size: 14px;

}


body {

  display: flex;

  justify-content: center;

  align-items: center;

  min-height: 50vh;

}

</style>


</head>

<body>


<nav>

  <ul>

    <li class="active"><a href="">网站首页</a></li>

    <li><a href="">关于我们</a></li>

    <li><a href="">产品介绍</a></li>

  </ul>

</nav>


<script>

var nav = $('nav');

var line = $('<div />').addClass('line');


line.appendTo(nav);


var active = nav.find('.active');

var pos = 0;

var wid = 0;


if(active.length) {

  pos = active.position().left;

  wid = active.width();

  line.css({

    left: pos,

    width: wid

  });

}


nav.find('ul li a').click(function(e) {

  if(!$(this).parent().hasClass('active')) {

    e.preventDefault();


    var _this = $(this);


    nav.find('ul li').removeClass('active');


    var position = _this.parent().position();

    var width = _this.parent().width();


    if(position.left >= pos) {

      line.animate({

        width: ((position.left - pos) + width)

      }, 300, function() {

        line.animate({

          width: width,

          left: position.left

        }, 150);

        _this.parent().addClass('active');

      });

    } else {

      line.animate({

        left: position.left,

        width: ((pos - position.left) + wid)

      }, 300, function() {

        line.animate({

          width: width

        }, 150);

        _this.parent().addClass('active');

      });

    }


    pos = position.left;

    wid = width;

  }

});

</script>


</body>

</html>

捕获.PNG

Correcting teacher:灭绝师太Correction time:2019-01-23 09:09:29
Teacher's summary:练习的比较全面,知识点归纳的很好,继续保持哦

Release Notes

Popular Entries