Native js to achieve seamless carousel effect

Native js to achieve seamless carousel effect

Native js realizes the carousel effect (seamless scrolling) for your reference. The specific content is as follows

Effect picture:

Code:

<!DOCTYPE html>
<html lang="en">
<!-- day07 7-10-14 -->
 
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <script src="./images1/20.animate.js"></script>
 <style>
 * {
  margin: 0;
  padding: 0;
 }
 
 li {
  list-style: none;
 }
 
 .focus {
  /*overflow: hidden;*/
  position: absolute;
  top: 100px;
  left: 200px;
  width: 721px;
  height: 455px;
  background-color: brown;
 }
 
 .prev,
 .next {
  display: none;
  position: absolute;
  top: 50%;
  margin-top: -15px;
  width: 20px;
  height: 30px;
  background-color: rgba(0, 0, 0, .3);
  text-decoration: none;
  color: #fff;
  line-height: 30px;
  text-align: center;
  font-size: 16px;
  z-index: 2;
 }
 
 .focus ul {
  /* The introduction of animation js files requires positioning*/
  position: absolute;
  width: 600%;
 }
 
 .focus ul li {
  float: left;
 }
 
 .prev {
  left: 0;
  border-top-right-radius: 15px;
  border-bottom-right-radius: 15px;
 }
 
 .next {
  right: 0;
  border-top-left-radius: 15px;
  border-bottom-left-radius: 15px;
 }
 
 .promo-nav {
  position: absolute;
  bottom: 10px;
  left: 60px;
  width: 200px;
  height: 18px;
  border-radius: 9px;
 }
 
 .promo-nav li {
  float: left;
  width: 10px;
  height: 10px;
  background-color: #fff;
  margin: 2px;
  border-radius: 50%;
 }
 
 .promo-nav .current {
  background-color: orange;
 }
 .focus ul li a img {
  width: 721px;
  height: 455px;
 }
 </style>
</head>
 
<body>
 <div class="focus">
 <ul>
  <li>
  <a href="#" ><img src="images1/focu01.jpg" alt=""></a>
  </li>
  <li>
  <a href="#" ><img src="images1/focu02.jpg" alt=""></a>
  </li>
 
  <li>
  <a href="#" ><img src="images1/focu03.jpg" alt=""></a>
  </li>
  <li>
  <a href="#" ><img src="images1/focu04.jpg" alt=""></a>
  </li>
 
 </ul>
 <!-- Left button -->
 <a href="javascript:;" class="prev">&lt;</a>
 <!-- Right button -->
 <a href="javascript:;" class="next">&gt;</a>
 
 <ol class="promo-nav">
 
 </ol>
 </div>
 <script>
 window.addEventListener('load', function() {
  var focus = document.querySelector('.focus');
  var prev = document.querySelector('.prev');
  var next = document.querySelector('.next');
  var focusWidth = focus.offsetWidth;
  //The mouse passes through focus.addEventListener('mouseenter', function() {
   prev.style.display = 'block';
   next.style.display = 'block';
   clearInterval(timer);
   timer = null; // clear the timer variable })
  //The mouse leaves focus.addEventListener('mouseleave', function() {
   prev.style.display = 'none';
   next.style.display = 'none';
   timer = setInterval(function() {
   next.click();
   }, 2000)
 
  })
  //3. Dynamically generate small circles. Generate as many small circles as there are pictures. var ul = focus.querySelector('ul');
  var ol = focus.querySelector('.promo-nav');
  // console.log(ul.children.length); 4
  for (var i = 0; i < ul.children.length; i++) {
  //Create a li
  var li = document.createElement('li');
  //Record the index number of the current small circle through custom attributes li.setAttribute('index', i);
  //Insert after ol ol.appendChild(li);
  //4. Click the small circle with the mouse to change its color (add the current class to the small circle and remove this class from the other small circles) (exclusive idea)
  //Directly bind the click event while generating the small circle li.addEventListener('click', function() {
   for (var i = 0; i < ol.children.length; i++) {
   ol.children[i].className = '';
   }
   this.className = 'current';
 
 
   //5. Click the small dot to move the picture. The ul is moved.
   //The distance ul moves is the index number of the small circle multiplied by the width of the image. Note that it is a negative value. //When we click on a small li, we get the index number of the current small li. var index = this.getAttribute('index');
   //When we click on a certain li, we give the index number of li to num
   num = index;
   //When we click on a certain li, we give the index number of li to index
   circle = index;
   console.log(index);
 
   animate(ul, -index * focusWidth, );
  })
 
  }
  //Set the color of the first li in ol to white ol.children[0].className = 'current';
  //6. Clone the first li and put it behind ul var first = ul.children[0].cloneNode(true);
  ul.appendChild(first);
  //7. Click the button on the right to scroll one picture var num = 0;
  var circle = 0;
  var flag = true;
  //Right button next.addEventListener('click', function() {
   if (flag) {
   flag = false; //Close the throttle first //5. If you reach the last copied image, ul will quickly restore left to 0 (seamless scrolling)
   if (num == ul.children.length - 1) {
    ul.style.left = 0;
    num = 0;
   }
   num++;
   animate(ul, -num * focusWidth, function() {
    flag = true;
   });
   //8. Click the button on the right and the small circle will change accordingly. Declare a variable to control the change of the small circle circle++;
   //If circle is equal to 4, it means that we have finished cloning the last picture and we will restore it if (circle == ol.children.length) {
    circle = 0;
   }
   // //Clear the remaining small circle class names// for (var i = 0; i < ol.children.length; i++) {
   // ol.children[i].className = '';
   // }
   // //Leave the current small circle current class name// ol.children[circle].className = 'current';
   circleChange();
   }
  })
  //Left button prev.addEventListener('click', function() {
  if (flag) {
   flag = false;
   //5. If you reach the last copied image, ul will quickly restore left to 0 (seamless scrolling)
   if (num == 0) {
   num = ul.children.length - 1;
   ul.style.left = -num * focusWidth + 'px';
 
   }
   num--;
   animate(ul, -num * focusWidth, function() {
   flag = true;
   });
   //8. Click the button on the right and the small circle will change accordingly. Declare a variable to control the change of the small circle circle--;
   //If circle is less than 0, the small circle will be changed to the fourth small circle if (circle < 0) {
   circle = ol.children.length - 1;
   }
   // Clear the remaining small circle class names // for (var i = 0; i < ol.children.length; i++) {
   // ol.children[i].className = '';
   // }
   // Leave the current small circle current class name // ol.children[circle].className = 'current';
   circleChange();
  }
  })
 
  function circleChange() {
  //Clear the rest of the small circle class names for (var i = 0; i < ol.children.length; i++) {
   ol.children[i].className = '';
  }
  //Leave the current small circle current class name ol.children[circle].className = 'current';
  }
 
  //10. Automatically play the slideshow var timer = setInterval(function() {
  next.click();
  }, 2000)
 
 })
 </script>
</body>
 
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript implements circular carousel
  • Several methods of implementing carousel images in JS
  • js to write the carousel effect
  • Implementing a simple carousel based on JavaScript
  • JavaScript imitates Jingdong carousel effect
  • JavaScript Dom implements the principle and example of carousel
  • js to achieve mobile carousel sliding switch
  • javascript to realize the scroll wheel carousel picture
  • Sample code for a simple seamless scrolling carousel implemented with native Js

<<:  Detailed explanation of compiling and installing MySQL 5.6 on CentOS and installing multiple MySQL instances

>>:  IIS 7.5 uses URL Rewrite module to achieve web page redirection

Recommend

How to modify the sources.list of Ubuntu 18.04 to Alibaba or Tsinghua mirror

1. Backup source list The default source of Ubunt...

Vue+flask realizes video synthesis function (drag and drop upload)

Table of contents We have written about drag and ...

How to implement parallel downloading of large files in JavaScript

Table of contents 1. HTTP Range Request 1.1 Range...

WeChat applet implements calculator function

WeChat Mini Programs are becoming more and more p...

Detailed explanation of MySQL database binlog cleanup command

Overview Today I will mainly share how to correct...

Things to note when writing self-closing XHTML tags

The img tag in XHTML should be written like this:...

JavaScript Basics Variables

Table of contents 1. Variable Overview 1.1 Storag...

uni-app WeChat applet authorization login implementation steps

Table of contents 1. Application and configuratio...

8 commands to effectively manage processes in Linux

Preface The role of process management: Determine...

Will mysql's in invalidate the index?

Will mysql's IN invalidate the index? Won'...

Problems and solutions when installing MySQL8.0.13 on Win10 system

Operating system: Window10 MySQL version: 8.0.13-...

Detailed explanation of the available environment variables in Docker Compose

Several parts of Compose deal with environment va...

MySQL 8.0.20 installation and configuration method graphic tutorial

MySQL download and installation (version 8.0.20) ...

Detailed explanation of Apache SkyWalking alarm configuration guide

Apache SkyWalking Apache SkyWalking is an applica...