jQuery图片轮播
2017-11-05 00:00
363 查看
![](https://static.oschina.net/uploads/space/2017/1105/144844_p7z0_3697586.png)
主要原理:利用绝对定位+overflow+CSS3动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图片轮播</title> <style> .bg{ width: 20.5em; min-height: 10em; margin: 0 auto; position: relative; overflow: hidden; background-color: white; } #picBox{ position: absolute; min-height: 10em; min-width: 150em; left: -40em; } .picBoxR{ animation: spinR 1s linear; } .picBoxL{ animation: spinL 1s linear; } #picBox div{ margin-right: 2px; float: left; text-align: center; background-color: darkblue; color: white; /*margin-right: 0.2em;*/ width: 20em; height: 10em; line-height: 10em; } .act{ position: absolute; top: 3.5em; background: rgba(0,0,0,0); } .act a{ color: yellow; outline: none; text-decoration: none; font-size: 2em; margin-left: 4px; } .act a:active{ color: white; background: rgba(0,0,0,0.3); } .act a:hover{ color: white; background: rgba(0,0,0,0.3); } .act:last-child{ left: 18em; } @keyframes spinR{ 0% { transform:translateX(0em); }100%{ transform:translateX(-20em); } } @keyframes spinL{ 0% { transform:translateX(0em); }100%{ transform:translateX(20em); } } </style> </head> <body> <div class="bg"> <div id="picBox"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </div> <!--两个方向箭头--> <div class="act"><a href="javascript:;" >⇦</a></div> <div class="act"><a href="javascript:;" >⇨</a></div> </div> <script src="../js/jquery-3.2.1.js"></script> <script> $('.act').click(function () { var curL = $('#picBox').css('left'); var animator = document.getElementById('picBox'); console.log('\n curL = ' + curL); var curlN = ''; if($(this).text() == '\u21E8' ){ // 点击向右 // 动态添加动画 //等待动画完成之后进行位移变换 animator.className = ''; setTimeout(function () { animator.className = 'picBoxR'; }, 0); setTimeout(function () { curlN = parseInt(curL)/16 - 20 + 'em'; $('#picBox').css('left',curlN); var curL2 = $('#picBox').css('left'); console.log('\n curL2 = ' + curL2); var $one_li = $("#picBox div:first-child"); var $four_li = $("#picBox div:last-child"); $one_li.insertAfter($four_li); $('#picBox').css('left','-40em'); }, 1000); }else if($(this).text() == '\u21E6'){ //点击向左 //动态添加动画 animator.className = ''; setTimeout(function () { animator.className = 'picBoxL'; }, 0); //等待动画完成之后进行位移变换 setTimeout(function () { //进行位移计算 并 位移 curlN = parseInt(curL)/16 + 20 + 'em'; $('#picBox').css('left',curlN); //位移完成之后立即进行前后调换 var last = $("#picBox div:last-child"); var first = $("#picBox div:first-child"); last.insertBefore(first); //回到起始位置 $('#picBox').css('left','-40em'); }, 1000); }else return false; }); </script> </body> </html>
相关文章推荐
- jQuery 简易版的无缝图片轮播切换
- jQuery实现简单的图片轮播
- jquery聚焦自动轮播图片
- jquery实现简单的图片轮播
- jQuery图片轮播插件——前端开发必看
- jQuery 3D旋转图片轮播插件imageflow.js
- JQuery 图片滚动或者div滚屏,适合多图轮播
- 又一个jquery轮播效果,焦点图,带数字显示序号,这个可以添加对应标题在图片上,支持主流浏览器
- [Web]Jquery圆点的图片轮播
- 12款经典的白富美型—jquery图片轮播插件—前端开发必备
- jQuery图片轮播的具体实现
- jquery特效之图片轮播
- jquery实现横向图片轮播特效代码分享
- 图片轮播 拷贝过去就可以了(自己改jquery-1.9.1.min.js路径)
- jquery图片轮播
- jquery(图片轮播)
- 基于jQuery可悬停控制图片轮播代码
- jQuery实现图片轮播
- 解决js动态插入JQuery轮播图片出现的问题
- JQuery插件之图片轮播插件–slideBox