JavaScript to achieve window display effect

JavaScript to achieve window display effect

This article shares the specific code of JavaScript to achieve the window display effect for your reference. The specific content is as follows

1. Build the scaffolding first

* {
            margin: 0;
            padding: 0;
        }
        
        .box {
            width: 800px;
            height: 190px;
            border: 1px solid #000;
            margin: 100px auto;
        }
        
        ul {
            list-style: none;
            display: flex;
        }
        
        ul img {
            vertical-align: top;
        }
        
        .progress {
            width: 100%;
            height: 30px;
            background: #ccc;
        }
        
        .progress>.line {
            width: 100px;
            height: 100%;
            background: orange;
            border-radius: 15px;
 }
<div class="box">
        <ul>
            <li>
                <img src="images/img1.jpg" alt="">
            </li>
            <li>
                <img src="images/img2.jpg" alt="">
            </li>
            <li>
                <img src="images/img3.jpg" alt="">
            </li>
            <li>
                <img src="images/img4.jpg" alt="">
            </li>
            <li>
                <img src="images/img5.jpg" alt="">
            </li>
            <li>
                <img src="images/img6.jpg" alt="">
            </li>
            <li>
                <img src="images/img7.jpg" alt="">
            </li>
            <li>
                <img src="images/img8.jpg" alt="">
            </li>
            <li>
                <img src="images/img9.jpg" alt="">
            </li>
            <li>
                <img src="images/img10.jpg" alt="">
            </li>
        </ul>
        <div class="progress">
            <div class="line"></div>
        </div>
</div>

2. Logical part

Get the element that needs to be operated

Calculate the width of ul

Set the width of the ul

Calculate the scroll bar width

Set the scroll bar width

Listen for mouse click events

  • Get the current position of the scroll bar
  • Get the position of the mouse pressed in the scroll bar

Listening for mouse movement events

  • Get the position of the mouse after moving in the scroll bar
  • Calculate the offset
  • Safety Check
  • Reposition the scroll bar
  • Calculate the scroll distance of the image
  • Reposition the image
 .box {
           overflow: hidden;
        }
        
        ul {
            position: relative;
        }
        
        .progress {
            position: relative;
        }
        
        .progress>.line {
            position: absolute;
            left: 0;
            top: 0;
  }
//1. Get the element to be operated const oUl = document.querySelector("ul");
const oItems = oUl.querySelectorAll("li");
const oProgress = document.querySelector(".progress");
const oLine = document.querySelector(".line");
const oBox = document.querySelector(".box");
 
//2. Calculate the width of ul const ulWidth = oItems[0].offsetWidth * oItems.length;
 
//3. Set the width of ul oUl.style.width = ulWidth + 'px';
 
//4. Calculate the width of the scroll bar // Scroll bar width / scroll bar scroll range = container width / content range const progressWidth = oProgress.offsetWidth;
const boxWidth = oBox.offsetWidth;
const lineWidth = boxWidth / ulWidth * progressWidth;
 
//5. Set the scroll bar width oLine.style.width = lineWidth + 'px';
// Calculate the maximum scrolling range of the scroll bar const maxLineX = progressWidth - lineWidth;
// Calculate the maximum scrollable range of the image const maxImgX = boxWidth - ulWidth;
 
 //6. Listen for mouse pressed events oLine.onmousedown = function(e) {

e = e || window.e;
//a. Get the current position of the scroll bar let begin = parseFloat(oLine.style.left) || 0;
 
//b. Get the position where the mouse is pressed in the scroll bar let mouseX = e.pageX - oBox.offsetLeft;
 
//7. Listen for mouse movement events oLine.onmousemove = function(e) {
e = e || window.e;
//c. Get the position of the mouse after moving in the scroll bar let moveMouseX = e.pageX - oBox.offsetLeft;
 
//d. Calculate the offset let offsetX = moveMouseX - mouseX + begin;
 
//e. Safety check offsetX = offsetX < 0 ? 0 : offsetX;
offsetX = offsetX > maxLineX ? maxLineX : offsetX;
 
 //f. Reset the scroll bar position oLine.style.left = offsetX + 'px';
 
//g. Calculate the scrolling distance of the image // Scroll bar scrolling distance / scroll bar maximum scrolling range = image scrolling distance / image maximum scrolling range // Scroll bar scrolling distance / scroll bar maximum scrolling range * image maximum scrolling range = image scrolling distance const imgOffsetX = offsetX / maxLineX * maxImgX;
 
// h. Reset the image position oUl.style.left = imgOffsetX + "px";
            };
        };
        document.onmouseup = function() {
            oLine.onmousemove = null;
} 

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:
  • js to achieve window display effect
  • JS realizes product window special effects

<<:  The most detailed installation and configuration of redis in docker (with pictures and text)

>>:  The pitfall of MySQL numeric type auto-increment

Recommend

Nginx location matching rule example

1. Grammar location [=|~|~*|^~|@] /uri/ { ... } 2...

Vue realizes dynamic progress bar effect

This article example shares the specific code of ...

Docker time zone issue and data migration issue

Latest solution: -v /usr/share/zoneinfo/Asia/Shan...

A question about border-radius value setting

Problem Record Today I was going to complete a sm...

Implementation of LNMP for separate deployment of Docker containers

1. Environmental Preparation The IP address of ea...

MySQL 5.6.27 Installation Tutorial under Linux

This article shares the installation tutorial of ...

Vue's Render function

Table of contents 1. Nodes, trees, and virtual DO...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

Detailed explanation of efficient MySQL paging

Preface Usually, a "paging" strategy is...

How to avoid data loop conflicts when MySQL is configured with dual masters

I wonder if you have ever thought about this ques...

Simple usage of MySQL temporary tables

MySQL temporary tables are very useful when we ne...

How to run the react project on WeChat official account

Table of contents 1. Use the a tag to preview or ...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...