js实现购物车放大镜效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			#small{
				width:300px;
				height: 300px;
				background: url(13.jpg);
				background-size: 300px 300px;
				position: absolute;
				left: 100px;
				top: 100px;
			}
			#shade{
				width: 100px;
				height: 100px;
				background: pink;
				position: absolute;
				opacity: 0.3;
				cursor: move;
				display: none;
			}
			#big{
				width: 400px;
				height: 400px;
				background: url(13.jpg);
				background-size: 1200px 1200px;
				position: absolute;
				top: 100px;
				left: 450px;
				display: none;
			}
		</style>
	</head>
	<body>
		<div id="small">
			<div id="shade"></div>
		</div>
		<div id="big"></div>
	</body>
</html>
<script type="text/javascript">
	class MagnifyingGlass{
		constructor(newSmall,newBig,newShade) {
		    this.small = newSmall;
			this.big = newBig;
			this.shade = newShade;
		}
		
		onmouseover(){
			let that = this;
			this.small.onmouseover=function(evt){
				let e = evt || event;
				that.big.style.display = "block";
				that.shade.style.display = "block";
			}
		}
		
		onmouseout(){
			let that = this;
			this.small.onmouseout=function(evt){
				let e = evt || event;
				that.big.style.display = "none";
				that.shade.style.display = "none";
			}
		}
		
		onmousemove(){
			let that = this;
			this.small.onmousemove=function(evt){
				let e = evt || event;
				let left = e.pageX - this.offsetLeft - that.shade.offsetWidth/2;
				let top = e.pageY - this.offsetTop - that.shade.offsetHeight/2;
				
				if(left < 0){
					left = 0;
				}
				if(top < 0){
					top = 0;
				}
				let maxLeft = that.small.offsetWidth-that.shade.offsetWidth;
				
				if(left > maxLeft){
					left = maxLeft;
				}
				let maxTop = that.small.offsetHeight-that.shade.offsetHeight;
				
				if(top > maxTop){
					top = maxTop;
				}
				
				that.shade.style.left=left + "px";
				that.shade.style.top=top+ "px";
				
				let x = that.big.offsetWidth*left/that.shade.offsetWidth;
				let y = that.big.offsetHeight*top/that.shade.offsetHeight;

				console.log(y);
				that.big.style.backgroundPositionX = -x + "px";
				that.big.style.backgroundPositionY = -y + "px";
			}
		}
		eventBind(){
			this.onmouseover();
			this.onmouseout();
			this.onmousemove();
		}
	}
	let oSmall = document.getElementById("small");
	let oBig = document.getElementById("big");
	let oShade = document.getElementById("shade");
	let oMg = new MagnifyingGlass(oSmall,oBig,oShade);
	oMg.eventBind();
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript购物车实现效果可以非常丰富和实用。通过使用JavaScript编写的购物车功能,可以构建一个类似于电子商务网站中常见的购物车系统。 首先,在网页的商品列表中,通过JavaScript代码将每个商品的信息进行绑定和处理。当用户点击将商品添加到购物车按钮时,JavaScript将捕获用户的点击事件,并从商品列表中获取相关信息,例如商品的id、名称、价格以及数量等。 接下来,JavaScript会将获取到的商品信息保存到一个购物车对象中,这个购物车对象可以是一个数组或者是一个json对象。购物车对象会在用户点击添加商品按钮时实时更新,以反映当前购物车中的商品情况。 同时,JavaScript还会对购物车进行一些额外的处理。例如,当用户点击购物车图标时,JavaScript会将购物车对象中的商品信息动态显示在购物车弹窗中,包括商品的名称、价格和数量等。用户还可以在购物车中修改商品数量、删除商品或者清空购物车等操作。 此外,JavaScript还可以实现一些购物车的功能增强。例如,当用户修改商品的数量时,JavaScript会自动更新购物车中的商品总价和数量,并将其实时展示给用户。另外,JavaScript还可以对购物车中的商品进行排序、搜索和筛选等操作,以提供更好的用户体验。 总之,通过使用JavaScript实现购物车功能,我们可以实现一个用户友好、交互性强的购物车系统,为用户提供便捷、高效的购物体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值