jd或者淘宝的具体商品有个放大镜的效果。虽然网上类似插件琳琅满目,应用到项目上有诸多不便,自己抽点时间自己写了个类似插件,积累下代码,何乐而不为呢!!let‘go:

  1. 打算把此特效封装成个插件,先把最基本的算法实现,然后再一步步封装吧:

最终实现效果:

html 代码:

<div id="Magnifier"></div>

css 代码:

 <style>
* {
margin:;
padding:;
}
</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" }
);
}

js-放大镜效果的更多相关文章

  1. 最全js 放大镜效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 原生js放大镜效果

    效果: 1.  鼠标放上去会有半透明遮罩.右边会有大图片局部图 2.  鼠标移动时右边的大图片也会局部移动 放大镜的关键原理: 鼠标在小图片上移动时,通过捕捉鼠标在小图片上的位置,定位大图片的相应位置 ...

  3. jS放大镜效果

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demo4.aspx.cs& ...

  4. 原生js实现的放大镜效果

    这是我用原生js写的放大镜效果,与各种各样的框架技术相比,我喜欢使用原生的js,在这里,想和大家一起谈谈原生和框架技术的理解与个人喜好. <!DOCTYPE HTML><html&g ...

  5. 原生js实现放大镜效果

    今天做任务的时候,有一个任务就是让实现电商网站常用的放大镜效果,类似于这样的效果,之前并没有做过这种放大镜效果,刚开始的思路是对图片进行裁剪,但是后来发现实在是难以实现,于是求助了万能的谷歌,发现一个 ...

  6. Magnifier.js - 支持鼠标滚轮缩放的图片放大镜效果

    Magnifier.js 是一个 JavaScript 库,能够帮助你在图像上实现放大镜效果,支持使用鼠标滚轮放大/缩小功能.放大的图像可以显示在镜头本身或它的外部容器中.Magnifier.js 使 ...

  7. JS 文本输入框放大镜效果

    JS 文本输入框放大镜效果 今天下午研究了下 "文本输入框放大镜效果" 当然KISSY官网也有这种组件 请看kissy demo 其实这种效果 对于很多童鞋来说 应该并不陌生!我今 ...

  8. jquery+js实现鼠标位移放大镜效果

    jQuery实现仿某东商品详情页放大镜效果 用jquery+js实现放大镜效果,效果大概如下图! 效果是不是大家很感兴趣,放大镜查看细节,下边大家可以详细看一看具体是怎么实现的.下边直接看代码! HT ...

  9. js、jquery实现放大镜效果

    在一些电商网站的商品详情页面,都会有放大镜效果,实现起来并不是很困难,今天用了两个小时,写了一个放大镜效果的实例,来分享给大家! 实现的效果大概是这个样子的 预览 先来看一下效果吧,点击下面的链接预览 ...

  10. js之放大镜效果

      HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

随机推荐

  1. vue概念

    Vue是单向数据流还是双向数据绑定? Vue是单向数据流不是双向数据绑定 Vue的双向数据绑定不过是语法糖(语法糖本质就是一种新的编码方式,并没有给语言增加新的功能.语法糖目的就是为了让代码更易读,更 ...

  2. php面试专题---10、网络协议考点

    php面试专题---10.网络协议考点 一.总结 一句话总结: 网络的考点其实就是这些:常见状态码,常见协议,osi七层模型,http和https 1.HTTP/1.1中,状态码200.301.304 ...

  3. Java-访问控制权限

    Java面向对象-访问控制权限 Java中,可以通过一些Java关键字,来设置访问控制权限: 主要有 private(私有), package(包访问权限),protected(子类访问权限),pub ...

  4. Mysql 链接数据库时区错误

    错误信息:Error querying database.  Cause: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ ...

  5. ArcGIS API for JavaScript(4.x)-加载天地图

    ArcGIS API for JavaScript(3.x)如何加载天地图<ArcGIS API for Javascript 加载天地图(经纬度投影) - 张凯强 - 博客园>这篇文章已 ...

  6. 像计算机科学家一样思考python-第1章 程序之道

    1.7调试 程序是很容易出错的.因为某种古怪的原因,程序错误被称为bug,而查捕bug的过程称为调试(debugging). 一个程序中可能出现3种类型的错误:语法错误.运行时错误和语义错误.对它们加 ...

  7. postman的下载和使用

    postman的下载 官网:https://www.getpostman.com/downloads/ 创建账号或者用谷歌浏览器账号登录 登录之后,进行接口测试,这里请求百度为例,然后点击send,就 ...

  8. 史上最全最常用的正则表达式(转自微信公众号:javascript)

    很多不太懂正则的朋友,在遇到需要用正则校验数据时,往往是在网上去找很久,结果找来的还是不很符合要求.所以我最近把开发中常用的一些正则表达式整理了一下,在这里分享一下.给自己留个底,也给朋友们做个参考. ...

  9. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  10. Mac下安装nodejs,然后安装Vue-devtools工具

    一.安装nodejs 1.这一步简单,只要上官网下载下来,直接按照提示安装就可以,mac版本的安装方法很简单. 下载nodejs的官方网址是:  nodejs.org    ,浏览器输入就可以跳转到了 ...