js实现单张图片平移切换效果
2019/6/27 20:37:32
本文主要是介绍js实现单张图片平移切换效果,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
由于个人不需要手动切换功能,因此把那部分的内容删了,主要是增加了无缝切换的效果。
原理也很简单,大概是一张图移动到左边以后,从底部移回最右,等待下一次循环。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.1.min.js"></script> </head> <body> <div class="wrapper"> <h1>jquer实现图片切换</h1> <div id="focus"> <ul> <!-- 这里有三个z-index的设置 --> <li><div class="switch_pic" style="z-index: 3;background: url('imgs/01.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="z-index: 2;background: url('imgs/02.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="z-index: 1;background: url('imgs/03.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="background: url('imgs/04.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="background: url('imgs/meiko2.jpg') center center;background-size: cover;"></div></li> <li><div class="switch_pic" style="background: url('imgs/meiko7.jpg') center center;background-size: cover;"></div></li> </ul> </div> </div> <script type="text/javascript"> $(function() { var sWidth = $("#focus").width(); var len = $("#focus ul li").length; var index = 0; var picTimer; var $pics = $("#focus ul li").find('.switch_pic');//获取所有图片 showPics(index); //网页打开立即执行一次动画 picTimer = setInterval(function() { index++; if(index == len) {index = 0;} showPics(index); },3000);//3000毫秒的间隔 //显示图片函数,根据接收的index值显示相应的内容 function showPics(index) { //普通切换 var nowLeft = -sWidth; //每次移动固定量 var $pic = $pics.eq(index);//获取当前要移出的图片 var $nexPic = $pics.eq((index+1)%len);//当前要移入的图片 var $nexnexPic = $pics.eq((index+2)%len);//下一个要移入的图片 $nexPic.css("left",sWidth);//下一个图片移动到最右 //当前要移出的图片开始左移,模式设为平滑"linear",速度和间隔一样 $pic.animate({"left":nowLeft},3000,"linear",function(){ // 当前图片完全移出后,重新设置z-index $pic.css("z-index",1); $nexPic.css("z-index",3); $nexnexPic.css("z-index",2); }); //当前要移入的图片同时左移 $nexPic.animate({"left":0},3000,"linear"); } }); </script> <style type="text/css"> *{margin:0;padding:0;} body{font-size:12px;color:#222;font-family:Verdana,Arial,Helvetica,sans-serif;background:#f0f0f0;} .clearfix:after{content: ".";display: block;height: 0;clear: both;visibility: hidden;} .clearfix{zoom:1;} ul,li{list-style:none;} img{border:0;} .wrapper{width:800px;margin:0 auto;padding-bottom:50px;} h1{height:50px;line-height:50px;font-size:22px;font-weight:normal;font-family:"Microsoft YaHei",SimHei;margin-bottom:20px;} #focus{width:450px;height:350px;overflow:hidden;position:relative;} #focus ul{height:380px;position:absolute;} #focus ul li{float:left;width:450px;height:350px;overflow:hidden;position:absolute;background:#000;} #focus ul li div{position:absolute;overflow:hidden;width: 450px;height: 350px;} </style> </body>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于js实现单张图片平移切换效果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-06vue 新建功能多条数据(还没和后端交互)还能看详情 数据是前端缓存到本地吗?-icode9专业技术文章分享
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode