js implements smooth carousel at the beginning and end of images

小云云
Release: 2018-01-15 14:16:45
Original
1342 people have browsed it

This article mainly brings you an article to achieve smooth carousel of the beginning and end of pictures (JS native method - throttling). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

First give the HTML code. Please note that the first picture 1.jpg is added to the end of the carousel picture list (#list), and the last picture 5.jpg is added to the header.


    首尾轮播   
  

<

>

Copy after login

Next, the css and js codes are given. You can adjust the parameters of the container and list according to the size, quantity, and width of the images as appropriate. This is also what you need to consider when encapsulating JS. parameters.


*{ margin: 0; padding: 0; } #container{ height: 400px; width: 500px; margin: 0 auto; position: relative; overflow: hidden; border: 1px solid black; } #list>p { float: left; } #list{ position: absolute; height: 400px; width: 3600px; } #list img{ height: 400px; width: 500px; } .arrow { user-select:none; position: absolute; top:150px; z-index: 2; background-color: #aaa; height: 100px; width: 80px; cursor: pointer; opacity: 0.5; display: none; line-height: 100px; text-align: center; color: #222; font-size: 3em; } #container:hover .arrow{ display: block; } #prev{ left:20px; } #next{ right: 20px; }
Copy after login

In JS, we can control the speed of image switching by changing the speed variable....Here we use element.style.transition to control the transition effect. .


window.onload=function(){ var container = document.getElementById('container'); var list = document.getElementById('list');//获取图片容器 var prev = document.getElementById('prev');//向前按钮 var next = document.getElementById('next');//向后按钮 var nowP = 1; //显示位置 var judge = null; //执行权 var speed = 0.1; // 切换速度 秒 prev.onclick=function(){ if(!judge){ judge = setTimeout(function(){ if(nowP==1){setTimeout(function(){ list.style.transition="left 0s"; list.style.left = "-2500px";  nowP = 5;},speed*1000);} //当到达图片表左边缘时与动画同步切换 list.style.transition = "left "+speed+"s"; move(500); nowP--; judge = null; },speed*1000); } }; next.onclick=function(){ if(!judge){ judge = setTimeout(function(){ if(nowP==5){setTimeout(function(){ list.style.transition="left 0s"; list.style.left = "-500px"; nowP = 1;},speed*1000);} //当到达图片表右边缘时与动画同步切换 list.style.transition = "left "+speed+"s"; move(-500); nowP++; judge = null; },speed*1000); } }; function move(num){ var term = parseInt(list.style.left) + num ; list.style.left = term+"px"; } var roll= setInterval(function(){ next.onclick(); },2000); container.onmouseenter=function(){ clearInterval(roll); }; container.onmouseleave=function() { roll=setInterval(function(){ next.onclick(); },2000); }; };
Copy after login

The following is a demonstration demo. In simple terms, the principle is to set a delayed execution time and animation transition when switching to the beginning and end of the picture table animation. The setTimeout function with the same time is used to perform instant switching, and then throttling is used to ensure the maximum switching speed (speed).

I am also a beginner in front-end. If it is helpful, please click on the recommendation.

Related recommendations:

Bootstrap Image Wheel How to implement the broadcast effect

Example of using JS to implement image carousel

jQuery simple and practical carousel implementation method

The above is the detailed content of js implements smooth carousel at the beginning and end of images. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!