js实现点击选项置顶动画效果

(编辑:jimmy 日期: 2024/11/15 浏览:2)

本文实例为大家分享了js实现点击选项置顶动画的具体代码,供大家参考,具体内容如下

js实现点击选项置顶动画效果

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8" />
 <title>js置顶动画</title>
 <style>
  *{margin:0;padding:0;}
  ul {width: 1000px;margin:100px auto;position:relative;}
  li {position:absolute;height: 100px;width:900px;font-size:30px;background:#fff;line-height:100px;border:1px solid #eee;list-style:none;padding-left:50px;margin-top:-1px;transition:all .8s ease;}
  li button {float:right;position:relative;top:40px;right:30px;}
 </style>
</head>
<body>
 <ul>
 <li data-index="0">
  <span>内容0</span>
  <button>置顶</button>
 </li>
 <li data-index="1">
  <span>内容1</span>
  <button>置顶</button>
 </li>
 <li data-index="2">
  <span>内容2</span>
  <button>置顶</button>
 </li>
 <li data-index="3">
  <span>内容3</span>
  <button>置顶</button>
 </li>
 <li data-index="4">
  <span>内容4</span>
  <button>置顶</button>
 </li>
 </ul>
</body>
<script>
 function $$(str) {return document.querySelectorAll(str);}
 // 初始化排序
 function intData () {
 for (let i = 0, len = $$('ul li').length; i < len; i++) {
  $$('ul li')[i].style.top = (i * 101) + 'px'; 
  $$('ul li')[i].style.zIndex = (i * 101); 
 }
 }
 function bindEvent () {
 for (let i = 0, len = $$('ul li').length; i < len; i++) {
  $$('ul li')[i].onclick = function() {
  // 将点击的节点追加到第一个
  let first = this.parentNode.firstChild;
  this.parentNode.insertBefore(this, first);
  setTimeout(() => {
   intData();
  }, 50);
  };
 }
 }
 intData();
 bindEvent();
</script>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。