JavaScript实现放大镜效果代码示例
//小图 dom对象 bigImg: "./image/bigImg.jpg", 做放大镜效果做重要的一点是, height: 350 }, //大图路径 bigDom: document.getElementsByClassName("bigBgImg")[0], height: config.divBigSize.height * config.smallSize.height / config.bigSize.height }; //小图style的计算值 config.smallComputedStyle = window.getComputedStyle(config.smallDom); //大图style的计算值 config.bigComputedStyle = window.getComputedStyle(config.bigDom); //移动方块style的计算值 config.moveComputedStyle = window.getComputedStyle(config.moveDom); initSmallImg(); initBigImg(); initMoveDiv(); //初始化小图 function initSmallImg() { config.smallDom.style.background = `url("${config.smallImg}") no-repeat left top/contain`; //设置背景图片 config.smallDom.onmousemove = function (e) { //鼠标移入事件 //展示移动小块 config.moveDom.style.display = "block"; var move = window.getComputedStyle(config.moveDom); //获取鼠标在小图中的坐标 var position = getPosition(e); //设置移动框的位置 setPosition(position); //展示大图框 config.bigDom.style.display = "block"; //大图框中展示部分大图 displayBigBgImgSize(); } config.smallDom.onmouseout = function () { //移动小块隐藏, divBigSize: { //大图框的尺寸 width: 540,需要的朋友可以参考下 JavaScript实现放大镜效果: !DOCTYPE html html lang="en" head meta charset="UTF-8" meta content="width=device-width, //大图 dom对象 moveDom: document.getElementsByClassName("move")[0], //小图路径 smallDom: document.getElementsByClassName("smallBgImg")[0],事件源是小图 return { //直接获取鼠标距离事件源的横坐标和纵坐标 x: e.offsetX。
bigSize: { //大图尺寸 width: 800, 这篇文章主要介绍了JavaScript实现放大镜效果代码示例,文中通过示例代码介绍的非常详细。
这四个之间的比例尺 黄色移动块 /小图 = 部分大图 / 大图 。
66。
201,要找到黄色移动块、小图、部分大图、大图, y: e.offsetY }; } else { //鼠标出现在移动框中, initial-scale=1.0" titleDocument/title style .smallBgImg { width: 350px; height: 350px; border: 1px solid #ccc; box-sizing: border-box; background-clip: padding-box; float: left; position: relative; cursor: pointer; } .move { border: 1px solid #ccc; box-sizing: border-box; background: rgba(165。
否则会超出小图 var left = parseInt(config.moveComputedStyle.left); var top = parseInt(config.moveComputedStyle.top); if (left 0) { //最左 config.moveDom.style.left = "0px"; } if (left config.smallSize.width - config.moveSize.width) { //最右 config.moveDom.style.left = config.smallSize.width - config.moveSize.width + "px"; } if (top 0) { //最上 config.moveDom.style.top = "0px"; } if (top config.smallSize.height - config.moveSize.height) { //最下 config.moveDom.style.top = config.smallSize.height - config.moveSize.height + "px"; } } //展示部分大图 function displayBigBgImgSize() { //移动框的left/小图width = 大图框的left/大图width var moveLeft = parseInt(config.moveComputedStyle.left); var moveTop = parseInt(config.moveComputedStyle.top); config.bigDom.style.backgroundPosition = `-${moveLeft*config.bigSize.width/config.smallSize.width}px -${moveTop*config.bigSize.height/config.smallSize.height}px`; } }()); /script /body /html index.html 效果展示: 代码中的大图片和小图片要自己找,对大家的学习或者工作具有一定的参考学习价值, height: 800 }。
事件源是移动框 return { x: e.offsetX + parseFloat(config.moveComputedStyle.left) + 1,大图隐藏 config.moveDom.style.display = config.bigDom.style.display = "none"; } } //初始化大图 function initBigImg() { config.bigDom.style.background = `url("${config.bigImg}") no-repeat`; //设置背景图片 } //初始化移动框 function initMoveDiv() { config.moveDom.style.width = config.moveSize.width + "px"; config.moveDom.style.height = config.moveSize.height + "px"; } //获取鼠标的坐标位置 function getPosition(e) { if (e.target == config.smallDom) { //若鼠标出现在小图中,并且替换掉代码中的图片路径, height: 540 } }; //根据比例尺计算移动框的宽高 移动框/小图尺寸 = 大框尺寸/大图尺寸 config.moveSize = { width: config.divBigSize.width * config.smallSize.width / config.bigSize.width, //移动方块的dom对象 smallSize: { //小图尺寸 width: 350。
//鼠标距离事件源的横坐标 + 事件源在smallDom中的left值 + 边框值 y: e.offsetY + parseFloat(config.moveComputedStyle.top) + //鼠标距离事件源的纵坐标 + 事件源在smallDom中的top值 + 边框值 } } } //设置移动方块的位置 function setPosition(position) { //鼠标要始终在移动方块中央位置 config.moveDom.style.left = position.x - parseFloat(config.moveComputedStyle.width) / 2 + "px"; config.moveDom.style.top = position.y - parseFloat(config.moveComputedStyle.height) / 2 + "px"; //要限制移动框的范围在小图中, 0.5); position: absolute; left: 0; top: 0; } .bigBgImg { width: 540px; height: 540px; border: 1px solid #ccc; box-sizing: border-box; background-clip: padding-box; float: left; margin-left: 10px; } .hidden { display: none; } /style /head body div div /div /div div /div script (function () { //基本信息配置 var config = { smallImg: "./image/smallImg.jpg",。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jz/wp/11674.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
ludou_se_only); } add_action(s
时间:2021-01-23
-
所以经常导致输错密码的
时间:2021-01-23
-
WordPress措施打开速度慢的
时间:2021-01-22
-
WordPress回覆评论自动添加
时间:2021-01-22
-
我们可以通过下面的代码
时间:2021-01-22
-
如何修改WordPress默认脚色
时间:2021-01-20
-
同样的修改文件就行
时间:2021-01-20
-
少数善意的转载者既使没
时间:2021-01-20
热门文章
-
WordPress新手安装教程(图文)
时间:2020-12-28
-
在设置菜单中增加WordPress私密 隐藏的全部
时间:2021-01-10
-
WordPress博客程序常见错误的解决方法
时间:2020-12-28
-
WordPress程序打开速度慢的三种解决方法
时间:2021-01-10
-
wordpress使用代码在每篇文章尾部添加版权
时间:2020-12-14
-
WordPress博客措施常见错误的办理要领
时间:2021-01-15
-
关于WordPress的SEO优化相关的一些PHP页面脚
时间:2021-01-07
-
WordPress措施打开速度慢的三种办理要领
时间:2021-01-22
-
详解WordPress中的头像缓存和署理中的缓存
时间:2021-01-15
-
少数善意的转载者既使没有看到版权信息
时间:2021-01-20
