javascript之放大镜的实现

<!DOCTYPE html>

<html>

<head lang="en">

<meta charset="UTF-8">

<title></title>

<!--<link rel="stylesheet" href="css/index.css">-->

<style>

img {

display: block;

}

* {

margin: 0;

padding: 0;

}

.box {

width: 350px;

height: 350px;

margin: 100px;

border: 1px solid #ccc;

position: relative;

}

.big {

width: 400px;

height: 400px;

position: absolute;

top: 0;

left: 360px;

overflow: hidden;

display: none;

}

.big img {

position: absolute;

}

.mask {

width: 100px;

height: 100px;

background: rgba(255, 255, 0, 0.4);

position: absolute;

top: 0px;

left: 0px;

cursor: move;

display: none;

}

.small {

position: relative;

}

</style>

</head>

<body>

<div class="box" id="box">

<div class="small" id="small">

<img src="../img/pic-list2.jpg" width="350" alt="" />

<div class="mask" id="mask"></div>

</div>

<div class="big" id="big">

<img src="../img/pic-list2.jpg" width="800" alt="" id="img" />

</div>

</div>

</body>

<script>

// 图片也是种资源,执行到img标签时,才去请求图片资源.js代码会继续执行

// 直接获取图片的宽高是不行的,因为图片资源没有加载完成.

window.onload = function () {

/**使用函数编程**/

+function () {

// 1===给small 绑定事件

$('.small').onmouseover = overHandler;

$('.small').onmouseout = outHandler;

$('.small').onmousemove = moveHandler;

}();

// 2===移入事件的回调函数

function overHandler() {

// 显示小黄块

$('.mask').style.display = 'block';

// 显示大图

$('.big').style.display = 'block';

}

// 3===移出事件的回调函数

function outHandler() {

$('.mask').style.display = 'none';

$('.big').style.display = 'none';

}

// 获取 box的定位

let boxLeft = $('.box').offsetLeft;

let boxTop = $('.box').offsetTop;

console.log(boxLeft,boxTop);

//获取大图的宽度和高度

let sw = $('.small').offsetWidth;

let sh = $('.small').offsetHeight;

console.log(sw,sh);

// console.log(sw, sh);

//4===鼠标移动事件

function moveHandler(eve) {

let mw = $('.mask').offsetWidth;

let mh = $('.mask').offsetHeight;

// 计算小黄块能够移动的最大left和top

let maskMaxL = sw - mw;

let maskMaxT = sh - mh;

// 获取鼠标相对于文档的坐标(没有滚动条和可视区的一致)

let mouseX = eve.pageX;//eve.clientX;

let mouseY = eve.pageY;//eve.clientY;

// console.log(mouseX, mouseY);

let maskX = mouseX - boxLeft - mw / 2;

let maskY = mouseY - boxTop - mh / 2;

// 判断不能超出左侧和上侧

if (maskX < 0) maskX = 0

if (maskY < 0) maskY = 0

// 判断不能超出右边界和下边界

// console.log(maskMaxL, maskMaxT);

if (maskX > maskMaxL) maskX = maskMaxL;

if (maskY > maskMaxT) maskY = maskMaxT

$('.mask').style.left = maskX + 'px'

$('.mask').style.top = maskY + 'px'

// 计算大图移动的最大位置

let bigMaxLeft = $('#big').offsetWidth - $('#img').offsetWidth;

let bigMaxTop = $('#big').offsetHeight - $('#img').offsetHeight;

// console.log(bigMaxLeft, bigMaxTop);

// 计算大图的实时位置且进行设置

$('#img').style.left = maskX / maskMaxL * bigMaxLeft + 'px'

$('#img').style.top = maskY / maskMaxT * bigMaxTop + 'px'

}

// 获取节点方法

function $(ele) {

let res = document.querySelectorAll(ele);

return res.length == 1 ? res[0] : res;

}

}

</script>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值