js-放大镜效果_html/css_WEB-ITnose
jd或者淘宝的具体商品有个放大镜的效果。虽然网上类似插件琳琅满目,应用到项目上有诸多不便,自己抽点时间自己写了个类似插件,积累下代码,何乐而不为呢!!let‘go:
- 打算把此特效封装成个插件,先把最基本的算法实现,然后再一步步封装吧:
最终实现效果:
html 代码:
<div id="Magnifier"></div>
css 代码:
<style> * { margin: 0; padding: 0; } </style>
貌似什么都没有,开始咱们强大的js之旅吧:
javascript 代码:
function createElement(MagnifierId, sImg, bImg) { var Magnifier = $(MagnifierId); Magnifier.style.position = 'relative'; //小图div var smallDiv = $Create("div"); smallDiv.setAttribute("id", "small"); smallDiv.style.position = "absolute"; //遮罩层 var mask = $Create("div"); mask.setAttribute("id", "mask"); mask.style.position = "absolute"; //镜片 var mirror = $Create("div"); mirror.setAttribute("id", "mirror"); mirror.style.opacity = 0.3; mirror.style.position = "absolute"; mirror.style.display = "none"; //小图 var smallImg = $Create("img"); smallImg.setAttribute("src", sImg); smallImg.setAttribute("id", "smallImg"); smallImg.onload = function () { //如果没设置放大镜的height或者width 根据小图大小设置放大镜大小 if (!Magnifier.offsetHeight) { Magnifier.style.width = this.offsetWidth+"px"; Magnifier.style.height = this.offsetHeight + "px"; } //遮罩层大小和小图一样 mask.style.opacity = "0"; mask.style.width = this.width + 'px'; mask.style.height = this.height + "px"; mask.style.zIndex = 2; bigDiv.style.left = this.width + 5 + "px"; bigDiv.style.top = "-5px"; } smallDiv.appendChild(mask); smallDiv.appendChild(mirror); smallDiv.appendChild(smallImg); //视窗 var bigDiv = $Create("div"); bigDiv.setAttribute("id", "big"); bigDiv.style.position = "absolute"; bigDiv.style.overflow = "hidden"; bigDiv.style.display = "none"; var bigImg = $Create("img"); bigImg.setAttribute("src", bImg); bigImg.setAttribute("id", "bigImg"); bigImg.style.position = "absolute"; bigDiv.appendChild(bigImg); Magnifier.appendChild(smallDiv); Magnifier.appendChild(bigDiv); } function setMagnifierStyle(mirrorStyle,shichuangStyle) { //mirror for (var item in mirrorStyle) { mirror.style[item] = mirrorStyle[item]; } for (var item in shichuangStyle) { $("big").style[item] = shichuangStyle[item]; } } function registerEvent() { $("mask").onmouseover = function () { $("big").style.display = "block"; mirror.style.display = "block"; } $("mask").onmouseout = function () { $("big").style.display = "none"; mirror.style.display = "none"; } $("mask").onmousemove = function (evt) { var oEvent = evt || event; var disX = oEvent.offsetX; var disY = oEvent.offsetY; var mirrorLeft = disX - mirror.offsetWidth / 2; var mirrorTop = disY - mirror.offsetHeight / 2; if (mirrorLeft < 0) { mirrorLeft = 0; } else if (mirrorLeft > mask.offsetWidth - mirror.offsetWidth) { mirrorLeft = mask.offsetWidth - mirror.offsetWidth; } if (mirrorTop < 0) { mirrorTop = 0; } else if (mirrorTop > mask.offsetHeight - mirror.offsetHeight) { mirrorTop = mask.offsetHeight - mirror.offsetHeight; } mirror.style.top = mirrorTop + "px"; mirror.style.left = mirrorLeft + "px"; var paX = mirrorTop / (mask.offsetHeight - mirror.offsetHeight); var paY = mirrorLeft / (mask.offsetWidth - mirror.offsetWidth); $("bigImg").style.top = -paX * ($("bigImg").offsetHeight - $("big").offsetHeight) + "px"; $("bigImg").style.left = -paY * ($("bigImg").offsetWidth - $("big").offsetWidth) + "px"; } } function $(id) { return document.getElementById(id); } function $Create(type) { return document.createElement(type); }
最后再 onload小小的调用一下:
window.onload = function () { createElement("Magnifier", "images/Magnifier/small.jpg", "images/Magnifier/big.jpg"); setMagnifierStyle({ "width": "30px", "height": "30px", "backgroundColor": "#fff" }, { "width": "250px", "height": "250px" }); registerEvent(); }
效果总算出来了耶,
2. 接下来咱们封装吧:
Magnifer类代码:
function Magnifier( MagnifierId, //放大镜容器ID sImg, //小图片src bImg, //大图片src mirrorStyle, //小图片里镜片样式,json格式数据 ViewStyle //预览视窗样式,json格式数据 ) { var _this = this; this.MagnifierContainer = null; //容器 this.smallDiv = null; //小图容器 this.mask = null; //小图遮罩层 this.mirror = null; //小图镜片 this.smallImg = null; //小图 this.bigDiv = null; //预览视图 this.bigImg = null; //大图 var init = function () { _this.MagnifierContainer = _this.$(MagnifierId); _this.createElement(sImg, bImg); _this.setMagnifierStyle(mirrorStyle, ViewStyle); _this.MainEvent(); } init(); } Magnifier.prototype.createElement = function (sImg, bImg) { var _this = this; var $Create = this.$Create; this.MagnifierContainer.style.position = 'relative'; //脱离文档流,视情况修改 this.smallDiv = $Create("div"); this.smallDiv.setAttribute("id", "small"); this.smallDiv.style.position = "absolute"; this.mask = $Create("div"); this.mask.setAttribute("id", "mask"); this.mask.style.position = "absolute"; this.mirror = $Create("div"); this.mirror.setAttribute("id", "mirror"); this.mirror.style.opacity = 0.3; this.mirror.style.position = "absolute"; this.mirror.style.display = "none"; this.smallImg = $Create("img"); this.smallImg.setAttribute("src", sImg); this.smallImg.setAttribute("id", "smallImg"); this.smallImg.onload = function () { //如果没设置放大镜的height或者width 根据小图大小设置放大镜大小 if (!_this.MagnifierContainer.offsetHeight) { _this.MagnifierContainer.style.width = this.offsetWidth + "px"; _this.MagnifierContainer.style.height = this.offsetHeight + "px"; } //遮罩层大小和小图一样 _this.mask.style.opacity = "0"; _this.mask.style.width = this.offsetWidth + 'px'; _this.mask.style.height = this.offsetHeight + "px"; _this.mask.style.zIndex = 2; _this.bigDiv.style.left = this.offsetWidth + 5 + "px"; _this.bigDiv.style.top = "-5px"; } this.smallDiv.appendChild(this.mask); this.smallDiv.appendChild(this.mirror); this.smallDiv.appendChild(this.smallImg); this.bigDiv = $Create("div"); this.bigDiv.setAttribute("id", "big"); this.bigDiv.style.position = "absolute"; this.bigDiv.style.overflow = "hidden"; this.bigDiv.style.display = "none"; this.bigImg = $Create("img"); this.bigImg.setAttribute("src", bImg); this.bigImg.setAttribute("id", "bigImg"); this.bigImg.style.position = "absolute"; this.bigDiv.appendChild(this.bigImg); this.MagnifierContainer.appendChild(this.smallDiv); this.MagnifierContainer.appendChild(this.bigDiv); } Magnifier.prototype.setMagnifierStyle = function (mirrorStyle, ViewStyle) { for (var item in mirrorStyle) { this.mirror.style[item] = mirrorStyle[item]; } delete item; for (var item in ViewStyle) { this.bigDiv.style[item] = ViewStyle[item]; } } Magnifier.prototype.MainEvent = function () { var _this = this; this.mask.onmouseover = function () { _this.bigDiv.style.display = "block"; _this.mirror.style.display = "block"; } this.mask.onmouseout = function () { _this.bigDiv.style.display = "none"; _this.mirror.style.display = "none"; } this.mask.onmousemove = function (evt) { var oEvent = evt || event; var disX = oEvent.offsetX || oEvent.layerX; //兼容ff var disY = oEvent.offsetY || oEvent.layerY; var mirrorLeft = disX - _this.mirror.offsetWidth / 2; var mirrorTop = disY - _this.mirror.offsetHeight / 2; if (mirrorLeft < 0) { mirrorLeft = 0; } else if (mirrorLeft > this.offsetWidth - _this.mirror.offsetWidth) { mirrorLeft = this.offsetWidth - mirror.offsetWidth; } if (mirrorTop < 0) { mirrorTop = 0; } else if (mirrorTop > this.offsetHeight - _this.mirror.offsetHeight) { mirrorTop = this.offsetHeight - _this.mirror.offsetHeight; } _this.mirror.style.top = mirrorTop + "px"; _this.mirror.style.left = mirrorLeft + "px"; var paX = mirrorTop / (this.offsetHeight - _this.mirror.offsetHeight); var paY = mirrorLeft / (this.offsetWidth - _this.mirror.offsetWidth); _this.bigImg.style.top = -paX * (_this.bigImg.offsetHeight - _this.bigDiv.offsetHeight) + "px"; _this.bigImg.style.left = -paY * (_this.bigImg.offsetWidth - _this.bigDiv.offsetWidth) + "px"; } } Magnifier.prototype.$ = function (id) { return document.getElementById(id); } Magnifier.prototype.$Create = function (type) { return document.createElement(type); }
最后在onload调用下:
window.onload = function () { new Magnifier( "Magnifier", "images/Magnifier/small.jpg", "images/Magnifier/big.jpg", { "width": "30px", "height": "30px", "backgroundColor": "#fff" }, { "width": "250px", "height": "250px" } ); }

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제









이 기사는 HTML & lt; Progress & Gt에 대해 설명합니다. 요소, 그 목적, 스타일 및 & lt; meter & gt의 차이; 요소. 주요 초점은 & lt; progress & gt; 작업 완료 및 & lt; meter & gt; Stati의 경우

이 기사는 HTML & LT; Datalist & GT에 대해 논의합니다. 자동 완성 제안을 제공하고, 사용자 경험을 향상시키고, 오류를 줄임으로써 양식을 향상시키는 요소. 문자 수 : 159

이 기사는 HTML & lt; meter & gt에 대해 설명합니다. 범위 내에 스칼라 또는 분수 값을 표시하는 데 사용되는 요소 및 웹 개발의 일반적인 응용 프로그램. & lt; meter & gt; & lt; Progress & Gt; 그리고 Ex

이 기사는 모바일 장치의 반응 형 웹 디자인에 필수적인 Viewport Meta Tag에 대해 설명합니다. 적절한 사용이 최적의 컨텐츠 스케일링 및 사용자 상호 작용을 보장하는 방법을 설명하는 반면, 오용은 설계 및 접근성 문제로 이어질 수 있습니다.

이 기사에서는 브라우저에서 직접 사용자 입력을 검증하기 위해 필요한, Pattern, Min, Max 및 Length 한계와 같은 HTML5 양식 검증 속성을 사용하는 것에 대해 설명합니다.

이 기사는 html5 & lt; time & gt; 시맨틱 날짜/시간 표현 요소. 인간이 읽을 수있는 텍스트와 함께 기계 가독성 (ISO 8601 형식)에 대한 DateTime 속성의 중요성을 강조하여 Accessibilit를 향상시킵니다.

기사는 HTML5 크로스 브라우저 호환성을 보장하기위한 모범 사례에 대해 논의하고 기능 감지, 점진적 향상 및 테스트 방법에 중점을 둡니다.

이 기사는 & lt; iframe & gt; 외부 컨텐츠를 웹 페이지, 공통 용도, 보안 위험 및 객체 태그 및 API와 같은 대안을 포함시키는 태그의 목적.
