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

Summary of the most commonly used knowledge points about ES6 new features

Table of contents 1. Keywords 2. Deconstruction 3...

How to load Flash in HTML (2 implementation methods)

First method : CSS code: Copy code The code is as ...

Simple usage example of MySQL 8.0 recursive query

Preface This article uses the new features of MyS...

Detailed explanation of Nginx proxy_redirect usage

Today, I encountered a little problem when I was ...

A brief analysis of the function calling process under the ARM architecture

Table of contents 1. Background knowledge 1. Intr...

React example of how to get the value of the input box

React multiple ways to get the value of the input...

Free tool to verify that HTML, CSS and RSS feeds are correct

One trick for dealing with this type of error is t...

Graphic tutorial on installing tomcat8 on centos7.X Linux system

1. Create the tomcat installation path mkdir /usr...

Let you understand how HTML and resources are loaded

All content in this blog is licensed under Creati...

Use javascript to create dynamic QQ registration page

Table of contents 1. Introduction 1. Basic layout...

Implementation of Docker building Maven+Tomcat basic image

Preface In Java programming, most applications ar...

JavaScript to achieve accordion effect

This article shares the specific code for JavaScr...

Four categories of CSS selectors: basic, combination, attribute, pseudo-class

What is a selector? The role of the selector is t...

7 interesting ways to achieve hidden elements in CSS

Preface The similarities and differences between ...