Native JS for Slide Wheel Map


Implementation Principle

Purely using html+css+JS
Discovery is relatively simple and does not require additional plug-ins or libraries
The core is to group the pictures into a sequence of rows, moving left and right, and overflow:hidden of the parent element to determine which pictures to display.
Simply draw:

Build basic interface

<div id="box">
            <img src="images/arrowleft.png" id="arrow-left">
            <img src="images/arrowright.png" id="arrow-right">
            <ul id="multi-circles">
            <div id="multi-images">
                <img src="images/1.jpg">
                <img src="images/2.jpg">
                <img src="images/3.jpg">
                <img src="images/4.jpg">
                <img src="images/5.jpg">
                <div class="clear"></div>

There are three main parts, two arrows to the left and right, a sequence of dots, and a sequence of pictures.
Use absolute positioning to lay them out, and z-index to determine their cascading relationships
Define the width and height of the picture as a css variable in advance to make it easy for each element to calculate and determine the height and width

    #box {
    overflow: hidden;
    #multi-circles {
    z-index: 2;
    #multi-images {
    z-index: 1;
    #arrow-left {
    position: absolute;
    z-index: 3;
    #arrow-right {
    #arrow-left {

Determine Picture Number

In order to rotate, we need to know which picture we should show now
Define the variable currentIndex in js to represent the sequence number of the currently displayed picture, initially 0
When you click an arrow or move the mouse over a dot, simply change the number

//dom operation first, get html components
var arrowLeft = document.getElementById("arrow-left");
var arrowRight = document.getElementById("arrow-right");
var multiImages = document.getElementById("multi-images");
var circles = document.getElementById("multi-circles").getElementsByTagName("li");
var box=document.getElementById("box");

//Bind events for arrows and dots
arrowLeft.addEventListener("click", preMove);
arrowRight.addEventListener("click", nextMove);
for (let i = 0; i < circles.length; i++) {
        circles[i].setAttribute("id", i);
        circles[i].addEventListener("mouseenter", overCircle);

//Slide through a dot
function overCircle() {
    currentIndex = parseInt(;
//left arrow
function preMove() {
    if (currentIndex != 0) {
    else {
        currentIndex = 4;
//right arrow
function nextMove() {
    if (currentIndex != 4) {
    else {
        currentIndex = 0;


Now that we know which picture we should show now, how do we show it?
As we have already said above, the principle of sliding is to change the position of the image sequence and move left and right.
Add the overflow:hidden of the parent element to display only the corresponding picture
So just write a function like this and add it to the previous events

function moveImage() { = -currentIndex * 400 + "px";

This is a hard jump for sliding
There are ways to write animate functions or jquery functions on the web
Here I think of the transition attribute directly using css3
Just add one sentence below the css class of the image sequence

#multi-images {
     transition: 1s;

You can slide naturally

Dot color

We want the corresponding dots to turn red when the picture is displayed
It's easy to change the color of the current dot, as long as the current Index variable helps
But turn the previous dot back white
True, you can turn all the dots white with a for loop first
But then there is redundant work, and we know that just change the color of a dot
So you define a new variable, preIndex, to record the previous image displayed
As long as in the previously bound event function, the first sentence is a shilling preIndex=currentIndex
Save to the previous number just before the picture number changes
Then add such a function at the end of the event

function changeCircleColor(preIndex, currentIndex) {
    circles[preIndex].style.backgroundColor = "rgb(240, 240, 240)";
    circles[currentIndex].style.backgroundColor = "rgb(245, 40, 40)";

Suspended Arrow

We want the left and right arrows to appear when the mouse is placed on the relay map
So first let the display of the arrow be none

#arrow-left {

Add mouseover event to box

box.addEventListener("mouseover",function() {"block";"block";

When the mouse is over the arrow, it becomes a click style

#arrow-left {


Basic work has been done so far
But we also want it to rotate automatically
Wheeling pauses when the mouse is placed on the wheeling map
So we set up a timer
When the mouse is over the box, clear the timer, and leave, re-establish

box.addEventListener("mouseover",function() {
box.addEventListener("mouseout",function() {


I encountered a very strange bug while making it
Event discoloration is also triggered when the mouse moves to the left of a dot
Later, I changed the light background and found that it was due to the default style of <li>
There is also a small dot to the left of the dot

Remove this style

li {
    list-style: none;

Complete Code

Code address:

Keywords: Javascript JQuery Attribute github

Added by Aptana on Wed, 15 May 2019 22:53:16 +0300