当前位置:   article > 正文

JS特效第153弹:jQuery响应式图片无缝切换代码_响应式 点击文字 图片切换

响应式 点击文字 图片切换

         jQuery响应式图片无缝切换代码是一款宽屏幻灯片轮播切换特效 ,先来看看效果:

        一部分关键的代码如下:

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>jQuery响应式图片无缝切换代码</title>
  8. <style type="text/css">
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. list-style-type: none;
  13. }
  14. .banner{
  15. width: 100%;
  16. /*height: 560px;*/
  17. /*background: black;*/
  18. /*子绝父相*/
  19. position: relative;
  20. /*超出部分全部隐藏,解决横向滚动条*/
  21. overflow: hidden;
  22. }
  23. .banner ul{
  24. position: absolute;
  25. }
  26. .banner ul li{
  27. /*这个100%是相对于父盒子的(会把父盒子的宽度继承过来,如父盒子300%,这这里的100%就会相当于300%)*/
  28. width: 100%;
  29. height: 560px;
  30. float: left;
  31. background: no-repeat center center;
  32. }
  33. /*.banner ul li:nth-child(1){*/
  34. /*background: url('./1.jpg') no-repeat center center;*/
  35. /*}*/
  36. /*.banner ul li:nth-child(2){*/
  37. /*background: url('./2.jpg') no-repeat center center;*/
  38. /*}*/
  39. /*.banner ul li:nth-child(3){*/
  40. /*background: url('./3.jpg') no-repeat center center;*/
  41. /*}*/
  42. .banner ol{
  43. /*width: 60px;*/
  44. height: 20px;
  45. background: rgba(0,0,0,0.5);
  46. position: absolute;
  47. left:50%;
  48. /*margin-left: -40px;*/
  49. bottom: 30px;
  50. padding: 0 10px;
  51. border-radius: 10px;
  52. }
  53. .banner ol li{
  54. width: 10px;
  55. height: 10px;
  56. float: left;
  57. margin: 5px 5px;
  58. background: rgba(255,255,255,0.5);
  59. border-radius: 50%;
  60. cursor: pointer;
  61. }
  62. .banner ol .current{
  63. background: rgba(255,255,255,1);
  64. }
  65. .banner i{
  66. width: 58px;
  67. height: 120px;
  68. position: absolute;
  69. top: 50%;
  70. margin-top: -60px;
  71. cursor: pointer;
  72. border-radius: 5px;
  73. display: none;
  74. }
  75. .banner .left{
  76. left: 60px;
  77. background: url('img/left_right.png') no-repeat 0 0px;
  78. }
  79. .banner .right{
  80. right: 60px;
  81. background: url('img/left_right.png') no-repeat 0px -120px;
  82. }
  83. .banner .left:hover , .banner .right:hover{
  84. background-color: rgba(0, 0, 0, 0.31);
  85. }
  86. </style>
  87. </head>
  88. <body>
  89. <div class="banner">
  90. <ul></ul>
  91. <ol></ol>
  92. <i class="left"></i><i class="right"></i>
  93. </div>
  94. <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
  95. <script type="text/javascript">
  96. $(function(){ //页面加载完毕才执行
  97. //=========设置参数==========
  98. //图片统一高度:
  99. var images_height = '560px';
  100. //图片路径/链接(数组形式):
  101. var images_url = [
  102. 'img/1.jpg',
  103. 'img/2.jpg',
  104. 'img/3.jpg'
  105. ];
  106. var images_count = images_url.length;
  107. //console.log(images_count);
  108. //创建节点
  109. //图片列表节点
  110. for(var j=0;j<images_count+1;j++){
  111. $('.banner ul').append('<li></li>')
  112. }
  113. //轮播圆点按钮节点
  114. for(var j=0;j<images_count;j++){
  115. if(j==0){
  116. $('.banner ol').append('<li class="current"></li>')
  117. }else{
  118. $('.banner ol').append('<li></li>')
  119. }
  120. }
  121. //载入图片
  122. $('.banner ul li').css('background-image','url('+images_url[0]+')');
  123. $.each(images_url,function(key,value){
  124. $('.banner ul li').eq(key).css('background-image','url('+value+')');
  125. });
  126. $('.banner').css('height',images_height);
  127. $('.banner ul').css('width',(images_count+1)*100+'%');
  128. $('.banner ol').css('width',images_count*20+'px');
  129. $('.banner ol').css('margin-left',-images_count*20*0.5-10+'px');
  130. //=========================
  131. var num = 0;
  132. //获取窗口宽度
  133. var window_width = $(window).width();
  134. $(window).resize(function(){
  135. window_width = $(window).width();
  136. $('.banner ul li').css({width:window_width});
  137. clearInterval(timer);
  138. nextPlay();
  139. timer = setInterval(nextPlay,2000);
  140. });
  141. //console.log(window_width);
  142. $('.banner ul li').width(window_width);
  143. //轮播圆点
  144. $('.banner ol li').mouseover(function(){//用hover的话会有两个事件(鼠标进入和离开)
  145. $(this).addClass('current').siblings().removeClass('current');
  146. //第一张图: 0 * window_width
  147. //第二张图: 1 * window_width
  148. //第三张图: 2 * window_width
  149. //获取当前编号
  150. var i = $(this).index();
  151. //console.log(i);
  152. $('.banner ul').stop().animate({left:-i*window_width},500);
  153. num = i;
  154. });
  155. //自动播放
  156. var timer = null;
  157. function prevPlay(){
  158. num--;
  159. if(num<0){
  160. //悄悄把图片跳到最后一张图(复制页,与第一张图相同),然后做出图片播放动画,left参数是定位而不是移动的长度
  161. $('.banner ul').css({left:-window_width*images_count}).stop().animate({left:-window_width*(images_count-1)},500);
  162. num=images_count-1;
  163. }else{
  164. //console.log(num);
  165. $('.banner ul').stop().animate({left:-num*window_width},500);
  166. }
  167. if(num==images_count-1){
  168. $('.banner ol li').eq(images_count-1).addClass('current').siblings().removeClass('current');
  169. }else{
  170. $('.banner ol li').eq(num).addClass('current').siblings().removeClass('current');
  171. }
  172. }
  173. function nextPlay(){
  174. num++;
  175. if(num>images_count){
  176. //播放到最后一张(复制页)后,悄悄地把图片跳到第一张,因为和第一张相同,所以难以发觉,
  177. $('.banner ul').css({left:0}).stop().animate({left:-window_width},500);
  178. //css({left:0})是直接悄悄改变位置,animate({left:-window_width},500)是做出移动动画
  179. //随后要把指针指向第二张图片,表示已经播放至第二张了。
  180. num=1;
  181. }else{
  182. //在最后面加入一张和第一张相同的图片,如果播放到最后一张,继续往下播,悄悄回到第一张(肉眼看不见),从第一张播放到第二张
  183. //console.log(num);
  184. $('.banner ul').stop().animate({left:-num*window_width},500);
  185. }
  186. if(num==images_count){
  187. $('.banner ol li').eq(0).addClass('current').siblings().removeClass('current');
  188. }else{
  189. $('.banner ol li').eq(num).addClass('current').siblings().removeClass('current');
  190. }
  191. }
  192. timer = setInterval(nextPlay,2000);
  193. //鼠标经过banner,停止定时器,离开则继续播放
  194. $('.banner').mouseenter(function(){
  195. clearInterval(timer);
  196. //左右箭头显示(淡入)
  197. $('.banner i').fadeIn();
  198. }).mouseleave(function(){
  199. timer = setInterval(nextPlay,2000);
  200. //左右箭头隐藏(淡出)
  201. $('.banner i').fadeOut();
  202. });
  203. //播放下一张
  204. $('.banner .right').click(function(){
  205. nextPlay();
  206. });
  207. //返回上一张
  208. $('.banner .left').click(function(){
  209. prevPlay();
  210. });
  211. });
  212. </script>
  213. <div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
  214. <p>适用浏览器:IE8360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗.</p>
  215. </div>
  216. </body>
  217. </html>

        全部代码:jQuery响应式图片无缝切换代码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/870961
推荐阅读
相关标签
  

闽ICP备14008679号