基于jquery实现左右按钮点击的图片切换效果(2)

<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title></title> <link type="text/css" href="https://www.jb51.net/css/style.css"> <script> window.onresize=function(){ var winWidth = document.body.clientWidth; if(winWidth <=1180){ body.className="grid-960"; }else if (winWidth<= 1410){ body.className="grid-1180"; }else if (winWidth>1410){ body.className="grid-1410"; }else { alert("do not know!"); } } </script> </head> <body> <script>//初始化状态显示样式判断,放在body后面 var winWidth = document.body.clientWidth; if (winWidth <=1180){ body.className="grid-960"; }else if (winWidth<= 1410){ body.className="grid-1180"; }else if (winWidth>1410){ body.className="grid-1410"; }else { alert("do not know!"); } </script> <div> <div> <h2>热门活动</h2> <div> <a href="javascript:void(0)"></a> <a href="javascript:void(0)" ></a> <ul> <li><a href="javascript:;"><img src="https://www.jb51.net/images/activity01-1410.jpg"></a></li> <li><a href="javascript:;"><img src="https://www.jb51.net/images/activity02-1410.jpg"></a></li> <li><a href="javascript:;"><img src="https://www.jb51.net/images/activity03-1410.jpg"></a></li> <li><a href="javascript:;"><img src="https://www.jb51.net/images/activity03-1410.jpg"></a></li> <li><a href="javascript:;"><img src="https://www.jb51.net/images/activity02-1410.jpg"></a></li> </ul> </div> </div> </div> </body> </html> <script src="https://code.jquery.com/jquery-latest.js"></script> <script> //首页图片滚动切换 (function($){ $.photolist=function(a){ var w_li = a.find("li").width(); var h_li = a.find("li").height(); var margin_li=parseInt(a.find("li").css("marginLeft")); var now = 0; var num = 0; var addli = 0; var lisize = a.find("ul li").size(); var htmlall = a.find("ul").html(); //判断每次滚动数量 /* var w_body = $("body").width(); if(w_body <=1170){ var num = 3; }else if(w_body<= 1380){ var num = 4; }else if(w_body>1380){ var num = 5; } */ var num=3; //判断需要添加的li节点数量 var reminder=lisize%num; if(lisize%num!=0){addli = num-reminder;} else{addli = 0;} addlist(); //点击滚动事件 photoscroll(); $(window).resize(function(){ //location.reload(); now = 0; addli = 0; a.find("ul").html(htmlall);//html内容还原初始值 a.find(".ps_next").show();//按钮样式初始化 a.find(".ps_pre").hide(); //判断每次滚动数量 /* var w_body = $("body").width(); if(w_body <=1170){ var num = 3; }else if(w_body<= 1380){ var num = 4; }else if(w_body>1380){ var num = 5; } */ var num=3; //判断需要添加的li节点数量 var reminder=lisize%num; if(lisize%num!=0){addli = num-reminder;} else{addli = 0;} addlist(); w_li = a.find("li").width(); margin_li=parseInt(a.find("li").css("marginLeft")); a.find("ul").css({"width":(w_li+margin_li*2)*(lisize+addli)}); a.find("ul").animate({"margin-left":0});//ul位置还原 btnshow(now,parseInt((lisize+addli)/num),lisize,num); }); function addlist(){ for(i=0;i<addli;i++){ var html = a.find("ul li").eq(i).html(); a.find("ul").append("<li>"+html+"</li>"); } a.find("ul").css({"width":(w_li+margin_li*2)*(lisize+addli)}); //console.log(a.find("ul li").size()); } function photoscroll(){ a.find(".ps_pre").on("click",function(){//console.log(num); now--; if(now >= 0){ a.find("ul").animate({"margin-left":-now*num*(w_li+margin_li*2)}); btnshow(now,parseInt((lisize+addli)/num),lisize,num); } }); a.find(".ps_next").on("click",function(){//console.log(num); now++; if(now < (lisize+addli)/num){ a.find("ul").animate({"margin-left":-now*num*(w_li+margin_li*2)}); btnshow(now,parseInt((lisize+addli)/num),lisize,num); } }); btnshow(now,parseInt((lisize+addli)/num),lisize,num); } /*** 参数说明: now:当前是第几组,默认是0 c:总共有几组 d:初始化时li的个数 e:每组显示li个数 ***/ function btnshow(now,c,d,e){ if(d<=e){//如果初始化时li的个数小于一组要显示的数,则不显示pre和next按钮 a.find(".ps_next").hide(); a.find(".ps_pre").hide(); }else if(now==0){//初始化now=0,显示第一组,只显示next a.find(".ps_next").show(); a.find(".ps_pre").hide(); }else if(now==c-1){//显示到最后一组,只显示pre a.find(".ps_next").hide(); a.find(".ps_pre").show(); }else{//显示中间组,pre和next都需要显示 a.find(".ps_next").show(); a.find(".ps_pre").show(); } } } })(jQuery); $.photolist($("#activity-slide")); </script>

css部分:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgzxyz.html