JavaScript学习之购物网站放大镜

放大镜——功能实现

分析

  • 事件 :

      1. 鼠标移入事件 : 显示 ( 焦点,大图)
      1. 鼠标移出事件 : 隐藏 ( 焦点,大图)
      1. 列表鼠标移入 : 切换图片;
  • 初始化布局的时候,放大镜和焦点框的比例很重要;

  • 目前定义为 1 : 0.6;

HTML代码

<div class="container">
    <!-- 图片部分 -->
    <div class="img">
        <img src="./images/1.jpg" alt="">
        <!-- 焦点框 -->
        <div class="focus"></div>
    </div>
    <!-- 图片列表 -->
    <div class="list">
        <span class="active">
            <img src="./images/1.small.jpg" alt="">
        </span>
        <span>
            <img src="./images/2.small.jpg" alt="">
        </span>
    </div>
    <!-- 大图部分 -->
    <div class="big-img">
        <img src="./images/1.big.jpg" alt="">
    </div>
</div>

CSS代码

*{
    margin: 0;
    padding: 0;
}
.container{
    width: 350px;
    margin-left: 100px;
    position: relative;
}
.img{
    width: 350px;
    height: 350px;
}
.img img{
    width: 100%;
    height: 100%;
}
.focus{
    width: 238px;
    height: 238px;
    border: 1px solid #aaa;
    background: 50% top no-repeat #fede4f;
    opacity: .5;     
    cursor: move; 
    position: absolute;
    left: 0;
    top: 0;
    z-index:9;
    display: none;
}
.big-img{
    width: 540px;
    height: 540px;
    position: absolute;
    left: 350px;
    top: 0;
    overflow: hidden;
    display: none;
}
.big-img img{
    width: 800px;
    height: 800px;
    position: absolute;
}
.list span{
    display: inline-block;
    border: 2px solid #fff;
    margin-left: 5px;
}


.list span.active{
    border-color: #f10;
}

.list span:hover{
    border-color: #f10;
}
.list {
    padding-left: 20px;
    padding-top: 20px;
}

JavaScript代码

class Magnifier{
    constructor(){
        this.img = document.querySelector(".img");
        this.focus = document.querySelector(".focus");
        this.big_img = document.querySelector(".big-img");
        this.big_bg = document.querySelector(".big-img img");
        this.scale = 0.6;
        // - 为了获取元素的偏移距离,计算正确的focus位置; 
        this.container = document.querySelector(".container");
        // 找到按钮元素; 
        this.btns = document.querySelectorAll(".list span");
        // 图片数据; 
        this.img_list = [
            {
                small : "./images/1.jpg",
                big : "./images/1.big.jpg"
            },
            {
                small : "./images/2.jpg",
                big : "./images/2.big.jpg"
            }
        ]
        
        this.init();
        
        this.c_off = {
            left : this.container.offsetLeft,
            top  : this.container.offsetTop,
        }
        // offset 家族是没办法测量 display 为none 的元素; 
        this.f_style = getComputedStyle( this.focus ),
        this.i_style = getComputedStyle( this.img ) 
        
        // 边界数据对象; 
        this.boundary = {
            x : {
                min : 0,
                max : parseInt(this.i_style.width) - parseInt(this.f_style.width)
            },
            y : {
                min : 0 ,
                max : parseInt(this.i_style.height) - parseInt(this.f_style.height)
            }
        }
        this.bindEvent();
    }
    init(){     
    
        this.focus.style.width  = 350 * this.scale + "px";
        this.focus.style.height = 350 * this.scale + "px";
        
        this.big_img.style.width  = 800 * this.scale + "px";
        this.big_img.style.height = 800 * this.scale + "px";
    }
    
    bindEvent(){
        this.img.addEventListener("mouseover" , ()=>{
            this.show();
        })
        this.img.addEventListener("mouseout" , ()=>{
            this.hide();
        })
        this.img.addEventListener("mousemove" , ( e ) => {
            this.move(e.clientX , e.clientY);
        })
        
        for(let i = 0 ; i < this.btns.length ; i ++){
            this.btns[i].addEventListener("mouseenter" , ()=>{
                this.change( i );
            })
        }
    }
    show(){
        this.focus.style.display = "block";
        this.big_img.style.display = "block";
    }     
    hide(){
        this.focus.style.display = "none";
        this.big_img.style.display = "none";
    }
    move( x , y ){
        // 如果使用offset会出现 鼠标获取位置的参照物和focus元素定位的参照物不一致; 
        // - 导致元素位移位置不正确; 
        
        x =  x - this.c_off.left - parseInt(this.f_style.width) / 2;
        y =  y - this.c_off.top - parseInt(this.f_style.height) / 2;
        
        
        //  边界检测 
        x = x <= this.boundary.x.min ? this.boundary.x.min : x ; 
        x = x >= this.boundary.x.max ? this.boundary.x.max : x ; 
        
        y = y <= this.boundary.y.min ? this.boundary.y.min : y ; 
        y = y >= this.boundary.y.max ? this.boundary.y.max : y ; 
        
        this.focus.style.left = x + "px";
        this.focus.style.top  = y + "px";
        
        
        this.big_bg.style.left = - x * ( 800 / 350 ) + "px";
        this.big_bg.style.top  = - y * ( 800 / 350 ) + "px";
    }
    
    change( index ){
        this.btns.forEach( ele => ele.classList.remove("active") );
        
        this.btns[index].classList.add("active");
        
        this.img.children[0].src = this.img_list[index].small;
        this.big_bg.src = this.img_list[index].big;
    }

}

var m = new Magnifier;

点击链接下载资源:
资源包链接

原创不易,转载请注明出处。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的JavaScript商品放大镜的实现方法: 1. HTML结构 ```html <div class="product-image"> <img src="product-image.jpg" alt="Product Image"> <div class="zoom-container"></div> </div> ``` 2. CSS样式 ```css .product-image { position: relative; width: 400px; height: 400px; } .product-image img { width: 100%; height: 100%; object-fit: cover; } .zoom-container { position: absolute; top: 0; right: -400px; width: 400px; height: 400px; border: 1px solid #ccc; overflow: hidden; display: none; } .zoom-container img { position: absolute; top: 0; left: 0; width: 800px; height: 800px; } ``` 3. JavaScript代码 ```javascript const productImage = document.querySelector('.product-image'); const zoomContainer = document.querySelector('.zoom-container'); const zoomImage = document.createElement('img'); zoomImage.src = 'product-image.jpg'; zoomContainer.appendChild(zoomImage); productImage.addEventListener('mousemove', function(e) { const x = e.pageX - this.offsetLeft; const y = e.pageY - this.offsetTop; const zoomX = x / this.offsetWidth * zoomImage.offsetWidth - zoomContainer.offsetWidth / 2; const zoomY = y / this.offsetHeight * zoomImage.offsetHeight - zoomContainer.offsetHeight / 2; zoomImage.style.transform = `translate(${-zoomX}px, ${-zoomY}px)`; zoomContainer.style.display = 'block'; }); productImage.addEventListener('mouseleave', function() { zoomContainer.style.display = 'none'; }); ``` 以上代码实现了一个简单的商品放大镜效果,当鼠标在商品图片上移动时,会在旁边显示一个放大的镜头,镜头中显示的是鼠标所在位置的放大图像。当鼠标离开商品图片时,放大镜头会消失。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山河不识

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

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

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

打赏作者

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

抵扣说明:

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

余额充值