本文實例講述了js控制多圖左右滾動切換效果。分享給大家供大家參考。具體如下:
這是一款純js實現點擊左右按鈕圖片自動左右平滑滾動,默認5個一組左右滾動,可以手動修改js參數更改滑動數量。
運行效果圖:-------------------查看效果-------------------
小提示:浏覽器中如果不能正常運行,可以嘗試切換浏覽模式。
為大家分享的js控制多圖左右滾動切換效果代碼如下
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>js控制多圖左右滾動切換效果</title> <link href="css/jiaobenzhijia.css" type="text/css" rel="stylesheet" /> <script type="text/javascript"> window.onload = function () { var oBtnLeft = document.getElementById("goleft"); var oBtnRight = document.getElementById("goright"); var oDiv = document.getElementById("indexmaindiv"); var oDiv1 = document.getElementById("maindiv1"); var oUl = oDiv.getElementsByTagName("ul")[0]; var aLi = oUl.getElementsByTagName("li"); var now = -5 * (aLi[0].offsetWidth + 13); oUl.style.width = aLi.length * (aLi[0].offsetWidth + 13) + 'px'; oBtnRight.onclick = function () { var n = Math.floor((aLi.length * (aLi[0].offsetWidth + 13) + oUl.offsetLeft) / aLi[0].offsetWidth); if (n <= 5) { move(oUl, 'left', 0); } else { move(oUl, 'left', oUl.offsetLeft + now); } } oBtnLeft.onclick = function () { var now1 = -Math.floor((aLi.length / 5)) * 5 * (aLi[0].offsetWidth + 13); if (oUl.offsetLeft >= 0) { move(oUl, 'left', now1); } else { move(oUl, 'left', oUl.offsetLeft - now); } } var timer = setInterval(oBtnRight.onclick, 5000); oDiv.onmouseover = function () { clearInterval(timer); } oDiv.onmouseout = function () { timer = setInterval(oBtnRight.onclick, 5000); } }; function getStyle(obj, name) { if (obj.currentStyle) { return obj.currentStyle[name]; } else { return getComputedStyle(obj, false)[name]; } } function move(obj, attr, iTarget) { clearInterval(obj.timer) obj.timer = setInterval(function () { var cur = 0; if (attr == 'opacity') { cur = Math.round(parseFloat(getStyle(obj, attr)) * 100); } else { cur = parseInt(getStyle(obj, attr)); } var speed = (iTarget - cur) / 6; speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); if (iTarget == cur) { clearInterval(obj.timer); } else if (attr == 'opacity') { obj.style.filter = 'alpha(opacity:' + (cur + speed) + ')'; obj.style.opacity = (cur + speed) / 100; } else { obj.style[attr] = cur + speed + 'px'; } }, 30); } </script> </head> <body style="background:#CCC;"> <div class="indexmaindiv" id="indexmaindiv"> <div class="indexmaindiv1 clearfix" > <div class="stylesgoleft" id="goleft"></div> <div class="maindiv1 " id="maindiv1"> <ul id="count1"> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/1.jpg" /></div> <div class="teanames">樂靜老師1</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/2.jpg" /></div> <div class="teanames">樂靜老師2</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/3.jpg" /></div> <div class="teanames">樂靜老師3</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/4.jpg" /></div> <div class="teanames">樂靜老師4</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/5.jpg" /></div> <div class="teanames">樂靜老師5</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/6.jpg" /></div> <div class="teanames">樂靜老師6</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/7.jpg" /></div> <div class="teanames">樂靜老師7</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/8.jpg" /></div> <div class="teanames">樂靜老師8</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/1.jpg" /></div> <div class="teanames">樂靜老師9</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/2.jpg" /></div> <div class="teanames">樂靜老師10</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/3.jpg" /></div> <div class="teanames">樂靜老師11</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/4.jpg" /></div> <div class="teanames">樂靜老師12</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/5.jpg" /></div> <div class="teanames">樂靜老師13</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/6.jpg" /></div> <div class="teanames">樂靜老師14</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> <li> <div class="playerdetail"> <div class="detailimg"><img src="images/7.jpg" /></div> <div class="teanames">樂靜老師15</div> <div class="teadetail">獲得新加坡南陽理工學院計算機學士學位擅長營銷學培訓</div> <a class="checkdetail" href="http://www.jb51.net"></a> </div> </li> </ul> </div> <div class="stylesgoright" id="goright"></div> </div> </div> </body> </html>
以上就是為大家分享的js控制多圖左右滾動切換效果代碼,希望大家可以喜歡。