1. <tfoot id='rGAKA'></tfoot>
    2. <legend id='rGAKA'><style id='rGAKA'><dir id='rGAKA'><q id='rGAKA'></q></dir></style></legend>

      <small id='rGAKA'></small><noframes id='rGAKA'>

      <i id='rGAKA'><tr id='rGAKA'><dt id='rGAKA'><q id='rGAKA'><span id='rGAKA'><b id='rGAKA'><form id='rGAKA'><ins id='rGAKA'></ins><ul id='rGAKA'></ul><sub id='rGAKA'></sub></form><legend id='rGAKA'></legend><bdo id='rGAKA'><pre id='rGAKA'><center id='rGAKA'></center></pre></bdo></b><th id='rGAKA'></th></span></q></dt></tr></i><div id='rGAKA'><tfoot id='rGAKA'></tfoot><dl id='rGAKA'><fieldset id='rGAKA'></fieldset></dl></div>

        <bdo id='rGAKA'></bdo><ul id='rGAKA'></ul>

      JS DOM实现鼠标滑动图片效果

      时间:2023-12-07

      <i id='djhT4'><tr id='djhT4'><dt id='djhT4'><q id='djhT4'><span id='djhT4'><b id='djhT4'><form id='djhT4'><ins id='djhT4'></ins><ul id='djhT4'></ul><sub id='djhT4'></sub></form><legend id='djhT4'></legend><bdo id='djhT4'><pre id='djhT4'><center id='djhT4'></center></pre></bdo></b><th id='djhT4'></th></span></q></dt></tr></i><div id='djhT4'><tfoot id='djhT4'></tfoot><dl id='djhT4'><fieldset id='djhT4'></fieldset></dl></div>
        <bdo id='djhT4'></bdo><ul id='djhT4'></ul>

              <tbody id='djhT4'></tbody>
              1. <legend id='djhT4'><style id='djhT4'><dir id='djhT4'><q id='djhT4'></q></dir></style></legend>

                <small id='djhT4'></small><noframes id='djhT4'>

              2. <tfoot id='djhT4'></tfoot>

                JS DOM实现鼠标滑动图片效果可以分为以下几个步骤:

                步骤一:HTML结构

                首先,需要在HTML中编写需要实现滑动效果的图片。例如:

                <div class="image-box">
                    <img src="image1.jpg">
                    <img src="image2.jpg">
                    <img src="image3.jpg">
                    <img src="image4.jpg">
                </div>
                

                步骤二:CSS样式

                给图片盒子设置固定宽高,并将图片设置为绝对定位,重叠在一起。例如:

                .image-box {
                    width: 400px;
                    height: 300px;
                    position: relative;
                }
                .image-box img {
                    position: absolute;
                    top: 0;
                    left: 0;
                    opacity: 0;
                    transition: .5s;
                    width: 100%;
                    height: 100%;
                }
                

                步骤三:JS代码实现鼠标移动滑动效果

                通过JS监听鼠标在图片盒子上的位置,然后根据位置计算出当前鼠标位置所对应的图片应该展示的透明度,并通过JS动态设置图片的透明度,实现滑动效果。例如:

                const imageBox = document.querySelector('.image-box');
                const images = imageBox.querySelectorAll('img');
                const length = images.length;
                
                imageBox.addEventListener('mousemove', event => {
                    const { x } = event;
                
                    images.forEach((image, index) => {
                        const opacity = (length - index) / length;
                        image.style.opacity = 1 - Math.abs(x - (index + 1) * imageBox.offsetWidth / length) / imageBox.offsetWidth * 2 + opacity;
                    });
                });
                

                以上是一个简单的实现鼠标滑动图片效果的方法。下面给出两个示例说明:

                示例一:

                为了让图片滑动更加流畅,可以对代码进行优化,例如添加节流函数。以下是一个实现滑动效果并加入函数节流的示例代码:

                function throttle(fn, delay) {
                    let lastTime = 0;
                
                    return function(...args) {
                        const nowTime = new Date().getTime();
                
                        if (nowTime - lastTime >= delay) {
                            fn.apply(this, args);
                            lastTime = nowTime;
                        }
                    }
                }
                
                const imageBox = document.querySelector('.image-box');
                const images = imageBox.querySelectorAll('img');
                const length = images.length;
                
                const mousemoveHandler = throttle(function(event) {
                    const { x } = event;
                
                    images.forEach((image, index) => {
                        const opacity = (length - index) / length;
                        image.style.opacity = 1 - Math.abs(x - (index + 1) * imageBox.offsetWidth / length) / imageBox.offsetWidth * 2 + opacity;
                    });
                }, 50);
                
                imageBox.addEventListener('mousemove', mousemoveHandler);
                

                示例二:

                除了鼠标滑动效果,还可以添加其他交互功能。例如,当鼠标移出图片盒子时,隐藏所有图片,只展示第一张图片。以下是一个实现鼠标移出时隐藏其他图片的示例代码:

                function throttle(fn, delay) {
                    let lastTime = 0;
                
                    return function(...args) {
                        const nowTime = new Date().getTime();
                
                        if (nowTime - lastTime >= delay) {
                            fn.apply(this, args);
                            lastTime = nowTime;
                        }
                    }
                }
                
                const imageBox = document.querySelector('.image-box');
                const images = imageBox.querySelectorAll('img');
                const length = images.length;
                
                const mousemoveHandler = throttle(function(event) {
                    const { x } = event;
                
                    images.forEach((image, index) => {
                        const opacity = (length - index) / length;
                        image.style.opacity = 1 - Math.abs(x - (index + 1) * imageBox.offsetWidth / length) / imageBox.offsetWidth * 2 + opacity;
                    });
                }, 50);
                
                const mouseleaveHandler = function() {
                    images.forEach((image, index) => {
                        image.style.opacity = index === 0 ? 1 : 0;
                    })
                }
                
                imageBox.addEventListener('mousemove', mousemoveHandler);
                imageBox.addEventListener('mouseleave', mouseleaveHandler);
                
                上一篇:js实现3D旋转相册 下一篇:jQuery实现连续动画效果实例分析

                相关文章

                • <bdo id='ryVNF'></bdo><ul id='ryVNF'></ul>
              3. <small id='ryVNF'></small><noframes id='ryVNF'>

              4. <tfoot id='ryVNF'></tfoot>
                <i id='ryVNF'><tr id='ryVNF'><dt id='ryVNF'><q id='ryVNF'><span id='ryVNF'><b id='ryVNF'><form id='ryVNF'><ins id='ryVNF'></ins><ul id='ryVNF'></ul><sub id='ryVNF'></sub></form><legend id='ryVNF'></legend><bdo id='ryVNF'><pre id='ryVNF'><center id='ryVNF'></center></pre></bdo></b><th id='ryVNF'></th></span></q></dt></tr></i><div id='ryVNF'><tfoot id='ryVNF'></tfoot><dl id='ryVNF'><fieldset id='ryVNF'></fieldset></dl></div>

                    <legend id='ryVNF'><style id='ryVNF'><dir id='ryVNF'><q id='ryVNF'></q></dir></style></legend>