JS实现图片放大镜功能

注意将.mask设置成pointer-events: none;  取消它的鼠标事件,否者鼠标移至图片时,会不停的mouseover与mouseout,出现闪动现象

<body>
    <div class='container'>
        <img class='small' src=".\1.jpg" alt="">
        <div class='mask'></div>
        <div class='rightView'>
            <img class='big' src=".\1.jpg" alt="">
        </div>
    </div>
</body>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .container {
        position: relative;
        top: 100px;
        left: 200px;
        width: 400px;
        height: 400px;
        border: 1px solid black;
    }

    .container .mask {
        background: rgba(50, 153, 248, 0.4);
        position: absolute;
        display: none;
        cursor: move;
        pointer-events: none;
    }

    .container .small {
        display: block;
        height: 400px;
        margin: 0 auto;
    }

    .container .rightView {
        display: none;
        position: absolute;
        left: 410px;
        top: 0;
        height: 400px;
        overflow: hidden;
    }

    .container .rightView .big {
        position: absolute;
    }
</style>
<script>
    var container = document.getElementsByClassName('container')[0]
    var rightView = document.getElementsByClassName('rightView')[0]
    var mask = document.getElementsByClassName('mask')[0]
    var bigImg = document.getElementsByClassName('big')[0]
    var smallImg = document.getElementsByClassName('small')[0]

    // 鼠标移至图片时,mask与rightView显示
    smallImg.addEventListener('mouseover', function () {
        rightView.style.display = 'block'
        mask.style.display = 'block'
    })

    // 鼠标移出图片时,mask与rightView消失
    smallImg.addEventListener('mouseout', function () {
        rightView.style.display = 'none'
        mask.style.display = 'none'
    })

    // 鼠标在图片上移动时的代码
    smallImg.addEventListener('mousemove', function (e) {
        e = e || window.event
        //设置rightView的宽度和mask的长宽
        rightView.style.width = smallImg.offsetWidth    
        mask.style.width = smallImg.offsetWidth * 2 / 5
        mask.style.height = smallImg.offsetHeight * 2 / 5
        //smallImg的高度在css中规定,对于不同的图片,左右会有一个空隙,mouseX的值要减去这个空隙值
        mouseX = e.pageX - container.offsetLeft - smallImg.offsetLeft - mask.offsetWidth / 2  
        mouseY = e.pageY - container.offsetTop - mask.offsetHeight / 2
        //鼠标移至smallImg的边缘时,mask应该保持在smallImg之内
        if (mouseX <= 0) {
            mouseX = 0
        }
        if (mouseY <= 0) {
            mouseY = 0
        }
        let maxScopeX = smallImg.offsetWidth - mask.offsetWidth
        let maxScopeY = smallImg.offsetHeight - mask.offsetHeight
        if (mouseX >= maxScopeX) {
            mouseX = maxScopeX
        }
        if (mouseY >= maxScopeY) {
            mouseY = maxScopeY
        }
        //mask是相对container定位的,要想不出smallImg,就要加上空隙的值
        mask.style.left = mouseX + smallImg.offsetLeft + 'px'
        mask.style.top = mouseY + 'px'
        //mask相对smallImg的位置等同于rightView相对于bigImg的位置,计算他们的比列就能使mask与rightView的显示区域相同
        imgProportion = (bigImg.offsetHeight - rightView.offsetHeight) / (smallImg.offsetHeight - mask.offsetHeight)
        bigImg.style.left = -imgProportion * mouseX + 'px'
        bigImg.style.top = -imgProportion * mouseY + 'px'
    })
</script>

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Vue 2的放大镜功能,你可以按照以下步骤进行操作: 1. 首先,确保你已经在项目中引入了Vue 2。 2. 在需要使用放大镜功能的组件中,定义一个data属性用于存储放大镜的状态,例如: ```javascript data() { return { zoomed: false, // 放大镜状态,默认为false mouseX: 0, // 鼠标X轴坐标 mouseY: 0, // 鼠标Y轴坐标 zoomX: 0, // 放大镜X轴坐标 zoomY: 0, // 放大镜Y轴坐标 } }, ``` 3. 在模板中,使用`v-on`指令监听鼠标移动事件,并更新鼠标坐标: ```html <template> <div @mousemove="updateMousePosition"> <!-- 图片展示区域 --> <div class="image-container"> <img src="path/to/image.jpg" alt="Image"> </div> <!-- 放大镜区域 --> <div v-if="zoomed" class="zoom-container" :style="{ left: zoomX + 'px', top: zoomY + 'px' }"> <img src="path/to/image.jpg" alt="Zoomed Image"> </div> </div> </template> ``` 4. 在方法中,实现更新鼠标坐标和放大镜位置的逻辑: ```javascript methods: { updateMousePosition(event) { this.mouseX = event.clientX; this.mouseY = event.clientY; this.updateZoomPosition(); }, updateZoomPosition() { const container = document.querySelector('.image-container'); const zoomContainer = document.querySelector('.zoom-container'); if (container && zoomContainer) { const containerRect = container.getBoundingClientRect(); const zoomContainerRect = zoomContainer.getBoundingClientRect(); // 根据鼠标坐标和图片容器位置计算放大镜位置 this.zoomX = this.mouseX - containerRect.left - zoomContainerRect.width / 2; this.zoomY = this.mouseY - containerRect.top - zoomContainerRect.height / 2; // 限制放大镜位置在图片容器内部 this.zoomX = Math.max(0, Math.min(this.zoomX, containerRect.width - zoomContainerRect.width)); this.zoomY = Math.max(0, Math.min(this.zoomY, containerRect.height - zoomContainerRect.height)); } } } ``` 5. 最后,添加一些CSS样式来定义图片容器和放大镜的样式: ```css .image-container { position: relative; } .zoom-container { position: absolute; width: 200px; /* 自定义放大镜的宽度 */ height: 200px; /* 自定义放大镜的高度 */ border: 1px solid #ccc; pointer-events: none; /* 防止放大镜影响鼠标事件 */ } ``` 这样,你就可以在Vue 2中实现一个简单的放大镜功能了。当鼠标在图片上移动时,会显示一个放大的镜头,跟随鼠标移动并显示放大的图像。根据需要,你可以自定义放大镜的样式和放大倍数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值