1. 每点击一次右箭头,图片区域向左滚动出一张图片,反之相同 2. 当发现图片滚动到末尾时,响应的箭头变成不可点击状态 3. 鼠标在图片区域内滑动滚轮,图片会随着鼠标滚轮的方向进行响应的滚动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>原生js slide</title> <style type="text/css"> *{padding: 0; margin: 0;} #slide{ width: 960px; height: 160px; position: relative; margin: 30px auto; border: 1px solid #ccc; } #slide .content{width: 960px; height: 160px; overflow: hidden; position: relative;} #slide ul{ position: absolute; -webkit-transition:all .27s ease-in; -moz-transition:all .27s ease-in; -o-transition:all .27s ease-in; transition:all .27s ease-in;} #slide li{list-style: none; float: left; width: 140px; height: 140px; margin: 10px; background: #c1c1c1; overflow: hidden;} #slide .next,#slide .prev{position: absolute; width: 28px; height: 28px; background: #999; overflow: hidden;} #slide .next{right: -28px; top: 66px;} #slide .prev{left: -28px; top: 66px;} </style> </head> <body> <div id="slide"> <div class="content"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> <li>13</li> <li>14</li> </ul> </div> <a class="next" id="next" href="javascript:;">next</a> <a class="prev" id="prev" href="javascript:;">prev</a> </div> <script type="text/javascript"> var obj = document.getElementById('slide'); var next = document.getElementById('next'), prev = document.getElementById('prev'), ul = obj.getElementsByTagName('ul')[0], liArr = obj.getElementsByTagName('li'), li_width = liArr[0].offsetWidth + 20, li_length = liArr.length, show_length = 6, index = 0; function slide(){ ul.style.width = li_width * li_length + 'px'; ul.style.left = 0; } function animation(){ if(index <= 0){ prev.style.cursor = 'default'; next.style.cursor = 'pointer'; index = 0; }else if(index >= (li_length - show_length)){ prev.style.cursor = 'pointer'; next.style.cursor = 'default'; index = (li_length -show_length); } var goal = li_width * -index; ul.style.left = goal + 'px'; } obj.onmousewheel = function(e){ var d = parseInt(e.wheelDelta)> 0 ? 1:-1; //判断滚动方向 index += 1*d; animation(); } next.onclick = function(e){ var target = e.target; prev.style.cursor = 'pointer'; index += 1; animation(); } prev.onclick = function(e){ var target = e.target; next.style.cursor = 'pointer'; index -= 1; animation(); } slide(); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>人人网</title> <style type="text/css"> .Roll{ width: 1200px; height: 200px; border: 1px solid #000000; padding-left: 28px; padding-right: 28px; overflow: hidden; position: relative; } .Roll ul{ width:6000px; height: 200px; margin: 0; padding: 0; overflow: hidden; position: absolute; } .Roll .roll-ul{ position: relative; height: 200px; overflow: hidden; border-left: 1px solid #000000; border-right: 1px solid #000000; } .Roll ul:after{ content: ''; clear: both; box-sizing: border-box; } .Roll ul li{ list-style: none; width: 300px; height: 200px; float: left; padding: 10px; box-sizing: border-box; } .Roll ul li img{ width: 280px; height: 180px; } .Roll .dj{ position: absolute; width: 100%; height: 45px; left: 0; top: 50%; margin-top: -22.5px; } .Roll .dj a{ position: absolute; width: 28px; height: 45px; top: 50%; margin-top: -22.5px; background-position: center; background-repeat: no-repeat; background-size: 28px 45px; } .Roll .dj .prev{ background-image: url(img/left1.png); left: 0; } .Roll .dj .next{ background-image: url(img/right1.png); right: 0; } </style> </head> <body> <div class="Roll" id="jsRoll"> <div class="roll-ul"> <ul> <li> <img src="img/b1.jpg"/> </li> <li> <img src="img/b2.jpg"/> </li> <li> <img src="img/b3.jpg"/> </li> <li> <img src="img/b4.jpg"/> </li> <li> <img src="img/b5.jpg"/> </li> <li> <img src="img/b6.jpg"/> </li> <li> <img src="img/b7.jpg"/> </li> </ul> </div> <div class="dj" id="arr"> <a href="javascript:;" id="prev" class="prev"></a> <a href="javascript:;" id="next" class="next"></a> </div> </div> <script type="text/javascript"> //获取元素 var box=document.getElementById("jsRoll"); var screenr=box.children[0]; var ul=screenr.children[0];//移动内容 var list=ul.children; var ol=screenr.children[1]; var arr=document.getElementById("arr"); var arrRight=document.getElementById("prev"); var arrLeft=document.getElementById("next"); var weiImge=list[0].offsetWidth; if(list.length == 7){ seamless(4); } //设置自动运动 timer=setInterval(function(){ //先进行判断是否走到最后一个 if (pic==list.length-4) { //复制第一张图片用来无缝连接 //回到开始状态 ul.style.left=0+"px"; pic=0; } if(pic<list.length-4) { pic++; var target=-pic*weiImge; animate(ul,{"left":target}); } }, 2000) //设置显示隐藏 box.onmouseover=function(){ arr.style.display="block"; //删除复制的无缝连接图片 if(list.length>7){ unSeamless(4) ul.style.left=0+"px"; pic=0; } clearInterval(timer); //兼容性写法,该函数也是网上别人写的,不过找不到出处了,蛮好的,所有我也没有必要修改了 //判断鼠标滚轮滚动方向 if (window.addEventListener)//FF,火狐浏览器会识别该方法 window.addEventListener('DOMMouseScroll', wheel, false); window.onmousewheel = document.onmousewheel = wheel;//W3C //统一处理滚轮滚动事件 function wheel(event){ var delta = 0; if (!event) event = window.event; if (event.wheelDelta) {//IE、chrome浏览器使用的是wheelDelta,并且值为“正负120” delta = event.wheelDelta/120; if (window.opera) delta = -delta;//因为IE、chrome等向下滚动是负值,FF是正值,为了处理一致性,在此取反处理 } else if (event.detail) {//FF浏览器使用的是detail,其值为“正负3” delta = -event.detail/3; } if (delta) handle(delta); } //上下滚动时的具体处理函数 function handle(delta) { if (delta <0){//向下滚动 rightSoll() }else{//向上滚动 leftSoll(); } } } box.onmouseout=function() { arr.style.display="none"; timer=setInterval(function(){ //先进行判断是否走到最后一个 if (pic==list.length-4) { if(list.length == 7){ seamless(4) } //回到开始状态 ul.style.left=0+"px"; pic=0; } if(pic<list.length-4) { pic++; var target=-pic*weiImge; animate(ul,{"left":target}); } }, 2000) } //创建复制无缝连接效果 function seamless (num){ for(var i=0;i<num;i++){ var firsImgt=list[i].cloneNode(true); ul.appendChild(firsImgt); } } //删除无缝效果 function unSeamless(num){ for(var i=0;i<num;i++){ ul.removeChild(list[list.length-1]) } } //设置右键点击 var pic=0; arrRight.onclick=function(){ //先进行判断是否走到最后一个 leftSoll(); } //向左移动 function leftSoll(){ if (pic==list.length-4) { //回到开始状态 //ul.style.left=0+"px"; //pic=0; arrRight.style.backgroundImage="url(img/left2.png)"; arrRight.style.pointerEvents="none"; } if(pic<list.length-4) { arrLeft.style.backgroundImage="url(img/right1.png)"; arrLeft.style.pointerEvents="auto"; pic++; var target=-pic*weiImge; animate(ul,{"left":target}); } } //左键点击 arrLeft.onclick=function(){ rightSoll(); } //向右移动 function rightSoll(){ if (pic==0) { //回到开始状态 //ul.style.left=-(ul.offsetWidth-weiImge)+"px"; //pic=list.length-1; arrLeft.style.backgroundImage="url(img/right2.png)"; arrLeft.style.pointerEvents="none"; } if (pic>0){ arrRight.style.backgroundImage="url(img/left1.png)"; arrRight.style.pointerEvents="auto"; pic--; var target=-pic*weiImge; animate(ul,{"left":target}); } } //创建移动的函数 function animate(tag,obj) { //清除定时器 clearInterval(tag.timer); tag.timer=setInterval(function (){ for (var k in obj) { var target=obj[k]//目标位置 //获取任意的样式属性 //如果未设置某个属性。取出了auto,转换为NaN,这时为了程序可以执行。使用短路操作 var leader=parseInt(getStyle(tag,k))||0; //获取步数 var step=(target-leader)/10; //根据步数的正负来判断方向 step=target>leader?Math.ceil(step):Math.floor(step); leader=leader+step; //设置相应的样式 tag.style[k]=leader+"px"; if (leader==target) { clearInterval(tag.timer); } } },20) } //获取相应属性 function getStyle(tag,attr) { return tag.currentStyle?tag.currentStyle[attr]:getComputedStyle(tag,null)[attr]; } </script> </body> </html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- *{padding: 0; margin: 0;}
- #slide{
- width: 1000px;
- height: 160px;
- position: relative;
- margin: 30px auto;
- border: 1px solid #ccc;
- }
- #slide:hover{
- cursor: pointer;
- }
- #slide .list{
- width: 960px;
- height: 160px;
- overflow: hidden;
- margin: 0 auto;
- position: relative;
- }
- #slide .list-item{
- list-style: none;
- float: left;
- width: 140px;
- height: 140px;
- margin: 10px;
- background: #c1c1c1;
- overflow: hidden;
- }
- .current{
- border:2px solid red;
- list-style: none;
- height: 160px;
- width: 50%;
- position: absolute;
- left: 25%;
- background: red;
- }
- #slide .next,#slide .prev{
- position: absolute;
- width: 30px;
- height: 28px;
- background: #999;
- overflow: hidden;
- text-decoration: none;
- }
- #slide .next{right: 0; top: 66px;}
- #slide .prev{left: 0; top: 66px;}
- </style>
- </head>
- <body>
- <div id="slide">
- <ul class="list" id="list">
- <li class="current">1</li>
- <li class="list-item">2</li>
- <li class="list-item">3</li>
- <li class="list-item">4</li>
- <li class="list-item">5</li>
- <li class="list-item">6</li>
- <li class="list-item">7</li>
- <li class="list-item">8</li>
- <li class="list-item">9</li>
- <li class="list-item">10</li>
- </ul>
- <a class="next" id="next" href="javascript:;">next</a>
- <a class="prev" id="prev" href="javascript:;">prev</a>
- </div>
- <script type="text/javascript">
- window.onload=function(){
- class Slider{
- constructor(id){
- this.container=document.getElementById(id);//获取图片容器
- this.items=this.container.querySelectorAll('li');
- }
- getSelectorItem(){ //获取当前展示的图片
- let selected=this.container.querySelector('.current');
- return selected;
- }
- getSelectorItemIndex(){ //获取当前选中的图片的索引
- return Array.from(this.items).indexOf(this.getSelectorItem());
- }
- slideTo(idx){ //跳转到idx这一张图片
- let selected=this.getSelectorItem();
- if(selected){
- selected.className="list-item";//将当前展示的图片设置'隐藏样式'
- }
- let item=this.items[idx];
- if(item){
- item.className="current";//将要展示的图片设置'显示样式'
- }
- }
- slideNext(){ //下一张
- let currentIdx=this.getSelectorItemIndex();
- //防止最后一张,使其循环显示
- let nextIdx=(currentIdx+1)%this.items.length;
- this.slideTo(nextIdx);
- }
- slidePrev(){ //上一张
- let currentIdx=this.getSelectorItemIndex();
- //防止第一张,使其循环显示
- let prevIdx=(this.items.length+currentIdx-1)%this.items.length;
- this.slideTo(prevIdx);
- }
- }
- //创建实例,绑定作用域,自动循环
- let slider=new Slider('list');
- let timer=setInterval(slider.slideNext.bind(slider),3000);
- let sli=document.getElementById('slide');
- let prev=document.getElementById('prev');
- let next=document.getElementById('next');
- sli.onmouseover=function(){ //清除定时器
- clearInterval(timer);
- };
- sli.onmouseout=function(){ //重设定时器
- timer=setInterval(slider.slideNext.bind(slider),3000);
- };
- sli.onmousewheel=function(e){
- //判断滚动方向
- parseInt(e.wheelDelta)>0?slider.slideNext():slider.slidePrev();
- }
- next.onclick=function(){ //下一张
- if (slider.getSelectorItemIndex()!==slider.items.length-1) {
- slider.slideNext();
- }else{
- next.style.cursor='default'; //图片滚动到末尾时不可点击
- }
- };
- prev.onclick=function(){ //前一张
- if (slider.getSelectorItemIndex()!==0) {
- slider.slidePrev();
- }else{
- prev.style.cursor='default'; //图片滚动到第一张时不可点击
- }
- };
- }
- </script>
- </body>
- </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>scrollImg</title> <style> *{ margin:0px; padding:0px;; } .container{ margin:30px auto; width:960px; height: 120px;; border: 1px solid black; position:relative; overflow:hidden; } .leftControl{ width:30px; height:118px; float:left; display:inline; } .leftControl img{ margin:48px 3px 48px 3px; width:24px; height:24px; } .imgContain{ width:898px; height:100px; margin:8px 0px 5px 0px; border: 1px solid black; float:left; display:inline; } .imgContain ul{ list-style: none; } .imgIn{ width:80px; height:80px; } li{ position:relative; float:left; width:80px; height:80px; background-color: #cccccc; margin:10px 0px 10px 29px; text-align: center; line-height: 80px; } .rightControl{ width:30px; height:118px; float:left; display:inline; } #rightA{ cursor:default; } .rightControl img{ margin:48px 3px 48px 3px; width:24px; height:24px; } </style> <script> var i=0; var c=8;//显示图片数 if(document.addEventListener){ document.addEventListener('DOMMouseScroll',mouseWheelHandler,false); }//W3C window.onmousewheel=document.onmousewheel=mouseWheelHandler;//IE/Opera/Chrome/Safari function mouseWheelHandler(e) { var direct = 0; e=e || window.event; //ie,opera,chrome if(e.wheelDelta){ if(e.wheelDelta==120){ //向上滚动事件 after(); } else{ //向下滚动事件 before(); } } //firefox if(e.detail){ if(e.detail==-3){ //向上滚动事件 after(); } else{ //向下滚动实践 before(); } } } function after(){ var lcount=document.getElementsByTagName("ul")[0]; var licount=lcount.getElementsByTagName("li"); if(i<licount.length-c){ licount[i].style.display="none"; i++; document.getElementById("rightA").style.cursor="pointer"; if(i==licount.length-c){ document.getElementById("leftA").style.cursor="default"; } } } function before(){ if(i>0){ var lcount=document.getElementsByTagName("ul")[0]; var licount=lcount.getElementsByTagName("li"); licount[i-1].style.display="inline"; i--; document.getElementById("leftA").style.cursor="pointer"; if(i==0){ document.getElementById("rightA").style.cursor="default"; } } } </script> </head> <body> <div class="container"> <div class="leftControl"> <a id="leftA" href="javascript:void(0);" onclick="after()"><img src="img/left.jpg"></a> </div> <div id="main" class="imgContain"> <ul> <li><img class="imgIn" src="img/1.jpg"></li> <li><img class="imgIn" src="img/3.jpg"></li> <li><img class="imgIn" src="img/left.jpg"></li> <li><img class="imgIn" src="img/right.jpg"></li> <li><img class="imgIn" src="img/1.jpg"></li> <li><img class="imgIn" src="img/3.jpg"></li> <li><img class="imgIn" src="img/left.jpg"></li> <li><img class="imgIn" src="img/right.jpg"></li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> <li>13</li> <li>14</li> <li>15</li> <li>16</li> <li>17</li> <li>18</li> </ul> </div> <div class="rightControl"> <a id="rightA" href="javascript:void(0);" onclick="before()"><img src="img/right.jpg"></a> </div> </div> </body> </html>