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

2023-12-07前端开发
21

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);
The End

相关推荐

layui实现图片上传成功后回显点击放大图片功能
layui实现图片上传成功后回显点击放大图片功能,html代码部分: !-- html代码--div class="layui-form-item" label class="layui-form-label"上传图片/label div class="layui-input-block" button type="button" class="layui-btn" id="license-auth-letter-...
2025-09-06 前端开发
202

Layui实现数据表格中鼠标悬停图片放大离开时恢复原图
Layui实现数据表格中鼠标悬停图片放大离开时恢复原图的效果,最终效果如下图所示: 实现代码如下,在done函数中调用hoverOpenImg方法 var tableIns = window.demoTable = table .render({ elem : '#idTest', id : 'idTest', url : '/postData', //width : 150...
2025-09-04 前端开发
112

layui点击文本输入框调起弹出选择框并选择内容的两种方法参考
我们在用到layui时候,需要点击文本输入框调起弹出选择框并选择内容,这个要怎么操作呢?以下两种方法可以参考: 1、点击名称,弹出信息弹框,选择表格中的某一行,实现效果如下: html页面代码 !--计量器具弹出层-- div id="equipment" lay-filter="equipmen...
2025-09-02 前端开发
167

网站部署https后百度地图不显示问题
https的网站如果引用百度地图,会出现加载不了的问题,这是因为涉及到跨域问题,网站是https的,但是引用百度地图的是http的,这个要怎么操作呢? 比如我引用的地址:http://api.map.baidu.com/api?v=2.0ak=AK显示 后来看了一下,少了一个s=1字段,加一下s=1...
2025-07-28 前端开发
139

微信小程序实现点击复制功能和手机拨打电话功能
做小程序项目的时候,客户提了一个功能需求优化,就是长按文字需要复制全部内容,因为有的手机支持全选复制,有的手机不支持全选复制。 通过设置系统剪贴板的内容和获取系统剪贴板的内容实现复制功能 html相关代码: van-field value="{{form.contactPhone}}"...
2025-07-02 前端开发
78

js拖拽排序插件Sortable.js如何使用
由于项目功能需要,要实现对table中的行实现拖拽排序功能,找来找去发现Sortable.js能很好的满足这个需求,而且它还是开源的,于是乎就开始学习使用Sortable.js 特点 轻量级但功能强大 移动列表项时有动画 支持触屏设备和大多数浏览器(IE9及以下除外) 支持...
2025-06-12 前端开发
161