首页 > web前端 > js教程 > 正文

js如何实现木马轮播图效果

王林
发布: 2020-03-30 11:24:21
转载
2434 人浏览过

js如何实现木马轮播图效果

首先,我们来看一下木马轮播图效果:

46327716d35058f52f40ee716d9cb78.png

具体代码如下:

(推荐教程:js教程

html部分代码:

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>旋转木马轮播图</title>
 <link rel="stylesheet" href="css/myStyle.css" rel="external nofollow" />
 <script type="text/javascript" src="js/animate.js"></script>
 <script type="text/javascript" src="js/my.js"></script>
</head>
<body>
<div id="wrap">
 <div id="slide">
  <ul>
   <li><a href="#"><img src="images/slidepic1.jpg" alt=""/></a></li>
   <li><a href="#"><img src="images/slidepic2.jpg" alt=""/></a></li>
   <li><a href="#"><img src="images/slidepic3.jpg" alt=""/></a></li>
   <li><a href="#"><img src="images/slidepic4.jpg" alt=""/></a></li>
   <li><a href="#"><img src="images/slidepic5.jpg" alt=""/></a></li>
  </ul>
  <div id="arrow">
   <a href="javascript:;" id="arrLeft"></a>
   <a href="javascript:;" id="arrRight"></a>
  </div>
 </div>
</div>
</body>
</html>
登录后复制

在html部分引入的myStyle.css文件部分代码

@charset "UTF-8";
 
blockquote,body,button,dd,dl,dt,fieldset,form,h1,h2,h3,h4, h5, h6, hr, input, legend, li, ol, p, pre, td, textarea, th, ul{
 margin:0;
 padding:0
}
 
body,button,input,select,textarea{
 font:12px/1.5 "Microsoft YaHei", "微软雅黑", SimSun, "宋体", sans-serif;
 color:#666;
}
 
ol,ul{
 list-style:none
}
 
a{
 text-decoration:none
}
 
fieldset,img{
 border:0;
 vertical-align:top;
}
 
a,input,button,select,textarea{
 outline:none
}
 
a,button{
 cursor:pointer
}
 
.wrap{
 width:1200px;
 margin:100px auto;
}
.slide{
 height:500px;
 position: relative;
}
 
.slide li{
 position:absolute;
 left:200px;
 top:0
}
 
.slide li img{
 width:100%
}
 
.arrow{
 opacity:0;
}
 
.prev ,.next{
 width:76px;
 height:112px;
 position:absolute;
 top:50%;
 margin-top:-56px;
 background:url(../images/prev.png) no-repeat;
 z-index:99;
}
 
 
.next{
 right:0;
 background-image:url(../images/next.png);
}
登录后复制

在html部分引入的animate.js文件部分代码

/**
 * Created by RENPINGSHENG on 2018/4/6.
 */
 
function animate(obj,json,fn) {
 clearInterval(obj.timer);
 obj.timer = setInterval(function () {
  var flag = true;
  for(var k in json){
   if( k == "opacity"){
    var leader = getStyle(obj,k) * 100;
    var target = json[k] * 100;
    var step = (target - leader) /10;
    step = step > 0 ? Math.ceil(step) : Math.floor(step);
    leader = leader + step;
    obj.style[k] = leader /100;
   } else if ( k == "zIndex"){
    obj.style[k] = json[k];
   }else{
    var leader = parseInt(getStyle(obj,k)) || 0;
    var target = json[k];
    var step = (target - leader) /10;
    step = step >0 ? Math.ceil(step) : Math.floor(step);
    leader = leader + step;
    obj.style[k] = leader + "px";
   }
 
   console.log("target:" + target + "leader:" + leader + "step:" + step);
   if (leader != target){
    flag = false;
   }
  }
 
  if (flag){
   clearInterval(obj.timer);
   if(fn){
    fn();
   }
  }
 },15)
}
 
function getStyle(obj,attr){
 if (obj.currentStyle){
  return obj.currentStyle[attr];
 }else{
  return window.getComputedStyle(obj,null)[attr];
 }
}
登录后复制

在html部分引入的my.js文件部分代码

/**
 * Created by RENPINGSHENG on 2018/4/6.
 */
 
 
window.onload = function () {
 var wrap = document.getElementById("wrap");
 var slide = document.getElementById("slide");
 var ul = slide.children[0];
 var lis = ul.children;
 var arrow = document.getElementById("arrow");
 var arrRight = document.getElementById("arrRight");
 var arrLeft = document.getElementById("arrLeft");
 
 var config = [
  {
   width:400,
   top:20,
   left:50,
   opacity:0.2,
   zIndex:2
  },
  {
   width:600,
   top:70,
   left:0,
   opacity:0.8,
   zIndex:3
  },
  {
   width:800,
   top:100,
   left:200,
   opacity:1,
   zIndex:4
  },
  {
   width:600,
   top:70,
   left:600,
   opacity:0.8,
   zIndex:3
  },
  {
   width:400,
   top:20,
   left:750,
   opacity:0.2,
   zIndex:2
  }
 ];
 
 wrap.onmouseover = function () {
  animate(arrow,{"opacity":1});
 }
 wrap.onmouseout = function () {
  animate(arrow,{"opacity":0});
 }
 function assign() {
  for(var i = 0;i < lis.length;i++){
   animate(lis[i],config[i],function(){
    flag = true;
   })
  }
 }
 
 var flag = true;
 assign();
 arrRight.onclick = function () {
  flag = false;
  config.push(config.shift());
  assign();
 };
 arrLeft.onclick = function () {
  flag = false;
  config.unshift(config.pop());
  assign();
 }
}
登录后复制

整个页面的文件结构如下图所示:

a8ce0d7ac0fb01529bb2c89900ef5f1.png

更多炫酷CSS3、html5、javascript特效代码,尽在:js特效大全

以上是js如何实现木马轮播图效果的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:jb51.net
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!