【demo】原生js实现淘宝放大镜

效果

在这里插入图片描述


BUG

  1. 受窗口移动影响(鼠标定位的时候)解决想法是在加上滚轮移动距离的变量 目前还没成功(。)
  2. 只能接受一个比例大小的图

代码

<div class="bigimg" id="small-box">
	<img src="img/1.jpg" alt="" width="400" height="200" id="big-img">
	<div class="tool" id="tool"></div>
</div>
<div class="big-glass" id="big-glass">
    <img src="img/1.jpg" width="800" height="500" alt="" id="big-pic">
</div>
.bigimg {
	display: inline-block;
	position: relative;
	height: 200px;
	width: 400px;
	background: #e5e5e5;
}
.bigimg img {
	position: absolute;
    margin: auto;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
	width: 100%;
}
.big-glass {
	position: absolute;
	overflow: hidden;
    float: left;
	top: 50px;
	left: 500px;
	height: 200px;
	width: 200px;
	display: none;
	z-index: 999;
}
.big-glass.active {
	display: block;
}
.big-glass img {
	height: 200%;
	position: absolute;
	top: 0;
	left: 0;
}
.tool {
	position: absolute;
	top: 0;
	left: 0;
	height: 100px;
	width: 100px;
	background: #e5e5e5;
	opacity: 0.6;
    filter: alpha(opacity=60);
    display: none;
}
.tool.active {
	display: block;
}
var smallBox = byId("small-box");
var bigBox = byId("big-glass");
var tool = byId("tool");

// 放大镜遮罩层
smallBox.onmouseenter = function(){
	tool.className = "tool active";
	bigBox.className = "big-glass active";
}
smallBox.onmouseleave = function(){
    tool.className = "tool";
    bigBox.className = "big-glass";
}
// 区域随着鼠标移动
smallBox.onmousemove = function(e){
    // 事件对象
    var _e = window.event||e;
    var x = _e.clientX - this.offsetLeft - tool.offsetWidth/2;
    var y = _e.clientY - this.offsetTop - tool.offsetHeight/2;
    // 左移出
    if(x < 0){
        x = 0;
    }
    // 上移出
    if(y < 0){
        y = 0;
    }
    // 右移出
    if(x>this.offsetWidth-tool.offsetWidth){
        x = this.offsetWidth-tool.offsetWidth;
    }
    // 下移出
    if(y>this.offsetHeight-tool.offsetHeight){
        y = this.offsetHeight-tool.offsetHeight;
    }
    tool.style.left = x + "px";
    tool.style.top = y + "px" ;
    bigPic.style.left = -x*2 + "px";
    bigPic.style.top = -y*2 + "px";
}

要点

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以提供一个简单的限时秒杀demo原生JavaScript实现。以下是基本的HTML和CSS代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>限时秒杀</title> <style> .seckill-item { border: 1px solid #ccc; margin: 10px; padding: 10px; width: 200px; height: 200px; float: left; } .seckill-item img { width: 100%; height: 120px; } .seckill-item h3 { font-size: 16px; margin: 10px 0; } .seckill-item .price { font-size: 20px; color: red; margin: 10px 0; } .seckill-item .stock { margin: 10px 0; } .seckill-item .btn-seckill { background-color: #f00; color: #fff; border: none; padding: 5px 10px; margin-top: 10px; cursor: pointer; } </style> </head> <body> <div id="seckill-list"> <div class="seckill-item"> <img src="img/1.jpg" alt="商品1"> <h3>商品1</h3> <div class="price">¥99.00</div> <div class="stock">库存: 100</div> <button class="btn-seckill" data-id="1" data-price="99.00" data-stock="100" data-start="2022/01/01 00:00:00" data-end="2022/01/01 23:59:59">立即秒杀</button> </div> <div class="seckill-item"> <img src="img/2.jpg" alt="商品2"> <h3>商品2</h3> <div class="price">¥199.00</div> <div class="stock">库存: 50</div> <button class="btn-seckill" data-id="2" data-price="199.00" data-stock="50" data-start="2022/01/01 00:00:00" data-end="2022/01/01 23:59:59">立即秒杀</button> </div> </div> <script src="seckill.js"></script> </body> </html> ``` 在这个页面中,我们设置了两个商品的信息,包括商品名称、价格、库存量和秒杀活动的开始时间和结束时间。每个商品都有一个“立即秒杀”按钮,它包含了商品的相关信息,通过data-*属性来存储。 以下是JavaScript代码的实现: ```javascript // 获取秒杀按钮列表 var seckillBtns = document.querySelectorAll('.btn-seckill'); // 循环遍历秒杀按钮列表,为每个按钮添加点击事件 seckillBtns.forEach(function(btn) { btn.addEventListener('click', function() { // 获取秒杀商品的相关信息 var id = this.getAttribute('data-id'); var price = this.getAttribute('data-price'); var stock = this.getAttribute('data-stock'); var start = this.getAttribute('data-start'); var end = this.getAttribute('data-end'); // 判断当前时间是否在秒杀活动时间范围内 var now = new Date(); var startTime = new Date(start); var endTime = new Date(end); if (now < startTime || now > endTime) { alert('秒杀活动还未开始或已经结束!'); return false; } // 判断商品库存是否充足 if (stock <= 0) { alert('商品库存不足!'); return false; } // 发送秒杀请求 alert('恭喜,秒杀成功!'); // 更新商品库存 stock--; this.setAttribute('data-stock', stock); this.parentNode.querySelector('.stock').innerHTML = '库存: ' + stock; }); }); ``` 这段代码的主要功能是为每个“立即秒杀”按钮添加点击事件,当用户点击按钮时,首先获取商品的相关信息,然后判断当前时间是否在秒杀活动时间范围内,以及商品库存是否充足。如果满足条件,就发送秒杀请求,更新商品库存信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值