商品信息页面放大镜功能

主要是针对商品进行放大功能

html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <link rel="stylesheet" href="css/glass.css">
</head>
<body>
    <div id="glassBox" >
        <div class="glassLeft">
            <div class="showBox">
                <img src="images/show_1.jpg" alt="">
                <div class="cover"></div>
            </div>
            <ul>
                <li class="active"><img src="images/small_1.jpg" alt=""></li>
                <li><img src="images/small_2.jpg" alt=""></li>
                <li><img src="images/small_3.jpg" alt=""></li>
                <li><img src="images/small_4.jpg" alt=""></li>
            </ul>
        </div>
        <div class="glassRight">
            <img src="images/big_1.jpg" alt="">
        </div>
    </div>
    <script src="js/glass.js"></script>
    <script>
        let glass =new Glass('#glassBox')
        glass.onMouseover()
        glass.onMouseout()
        glass.showSCale()
        glass.onGlass()
        glass.onTab()
    </script>
</body>
</html>

css代码

*{
    margin: 0;
    padding: 0;
}
ul,li{
    list-style: none;
}
#glassBox{
    width: 1200px;
    margin: 50px auto;
    display: flex;
}
.glassLeft .showBox{
    width: 350px;
    position: relative;
    margin-bottom: 20px;
    cursor: pointer;
}
.glassLeft .showBox>.cover{
    width: 100px;
    height: 100px;
    background-color: rgba(172, 255, 47, 0.5);
    position: absolute;
    top: 0;
    left: 0;
    display: none;
    pointer-events: none;
}
.glassLeft ul{
    display: flex;
    justify-content: space-between;
}
.glassLeft ul>li{
    cursor: pointer;
}
.glassLeft ul>li>img{
    width: 100%;
    height: 100%;
}
.glassRight{
    width: 600px;
    height: 600px;
    border: 1px solid #11c9bf;
    overflow: hidden;
    position: relative;
    margin-left: 40px;
    display: none;
}
.glassRight img{
    position: absolute;
}
.showActive{
    display: block !important;
}
ul>.active{
    border: 2px solid red;
}

js代码

/**
 * !使用class类实现
 *  *移动比例计算
 *  ? 覆盖层移动距离       覆盖层大小
 *  ? --------------  =  -----------
 *  ? 大图移动距离?        放大镜大小
 * * 显示比例计算
 *  ?    覆盖层大小         放大镜大小
 *  ? --------------  =  ----------------
 *  ?    显示盒子大小       大图显示区域大小?
 */

class Glass {
    constructor(id) {
        this.glassRoot = document.querySelector(id)
        this.showBox = this.glassRoot.querySelector('.showBox')
        this.coverBox = this.glassRoot.querySelector('.cover')
        this.ulEle = this.glassRoot.querySelector('.glassLight>ul')
        this.bigImg = this.glassRoot.querySelector('.glassRight>img')
        this.liEle = this.glassRoot.querySelectorAll('ul>li')
        this.showImg = this.glassRoot.querySelector('.showBox>img')
        this.glassBox = this.glassRoot.querySelector('.glassRight')
    }
    /**
     * !显示比列
     */
    showSCale() {
        // 放大镜的大小
        let glassBoxWidth = parseInt(window.getComputedStyle(this.glassBox).width)
        let glassBoxHeight = parseInt(window.getComputedStyle(this.glassBox).height)

        // 覆盖层的大小
        let coverBoxWidth = parseInt(window.getComputedStyle(this.coverBox).width)
        let coverBoxHeight = parseInt(window.getComputedStyle(this.coverBox).height)

        // 显示盒子的大小
        let showBoxWidth = parseInt(window.getComputedStyle(this.showBox).width)
        let showBoxHeight = parseInt(window.getComputedStyle(this.showBox).height)

        // 计算显示比例
        let w = glassBoxWidth * showBoxWidth / coverBoxWidth
        let h = glassBoxHeight * showBoxHeight / coverBoxHeight

        //设置大图片的大小
        this.bigImg.style.width = w + 'px'
        this.bigImg.style.height = h + 'px'
    }
    /**
     * !鼠标移入移出事件
     * *1.鼠标移入显示框,覆盖层出现,大图出现
     * *2.鼠标移出显示框,覆盖层隐藏,大图隐藏
     */
    onMouseover() {
        let _this = this
        this.showBox.addEventListener('mouseover', function () {
            _this.coverBox.classList.add('showActive')
            _this.glassBox.classList.add('showActive')
        })
    }
    onMouseout() {
        let _this = this
        this.showBox.addEventListener('mouseout', function () {
            _this.coverBox.classList.remove('showActive')
            _this.glassBox.classList.remove('showActive')
        })
    }
    /**
     * 放大镜效果
     */
    onGlass() {
        let _this = this
        this.showBox.addEventListener('mousemove', function (e) {
            e = e || window.event
            let x = e.offsetX - _this.coverBox.offsetWidth / 2
            let y = e.offsetY - _this.coverBox.offsetHeight / 2

            //边界检查
            if (x < 0) {
                x = 0
            }
            if (x > _this.showBox.offsetWidth - _this.coverBox.offsetWidth) {
                x = _this.showBox.offsetWidth - _this.coverBox.offsetWidth
            }
            if (y < 0) {
                y = 0
            }
            if (y > _this.showBox.offsetHeight - _this.coverBox.offsetHeight) {
                y = _this.showBox.offsetHeight - _this.coverBox.offsetHeight
            }
            // 覆盖层移动距离
            _this.coverBox.style.top = y + 'px'
            _this.coverBox.style.left = x + 'px'

            /**
             * !计算移动比例,从而计算出大图需要移动距离
             */

            // 放大镜的大小
            let glassBoxWidth = parseInt(window.getComputedStyle(_this.glassBox).width)
            let glassBoxHeight = parseInt(window.getComputedStyle(_this.glassBox).height)

            // 覆盖层的大小
            let coverBoxWidth = parseInt(window.getComputedStyle(_this.coverBox).width)
            let coverBoxHeight = parseInt(window.getComputedStyle(_this.coverBox).height)

            // 计算大图移动距离
            let bigImgX = x * glassBoxWidth / coverBoxWidth
            let bigImgY = y * glassBoxHeight / coverBoxHeight

            //移动大图
            _this.bigImg.style.top = -bigImgY + 'px'
            _this.bigImg.style.left = -bigImgX + 'px'
        })
    }
    /**
     * !选项卡
     *  * 1.鼠标移入发送改变,并同时清除所有选中效果
     *  * 2.给当前的选中图片设置选中效果
     *  * 3.切换显示图片和大图
     */
    onTab() {      
        for (let i = 0; i < this.liEle.length; i++) { 
            let _this = this 
            this.liEle[i].addEventListener('mouseover', function (){
                _this.onClear()
                this.classList.add('active')
                _this.showImg.setAttribute('src',`images/show_${i+1}.jpg`)
                _this.bigImg.setAttribute('src',`images/big_${i+1}.jpg`)         
            })
        }
    }
    onClear() {
        for (let i = 0; i < this.liEle.length; i++) {
            this.liEle[i].classList.remove('active')
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

一心就想回农村

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值