JQ面向对象的放大镜

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<div class="box">
<div class="smallBox">
<span class="mark"></span> <!--遮罩层-->
<span class="floa"></span>
<img src="img/small.jpg" />
</div>
<div class="bigBox">
<img src="img/big.jpg" />
</div>
</div>

//先导入jq库
<script src="js/jquery-1.11.3.js" type="text/javascript" charset="utf-8"></script>
<script src="js/fdj.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">

//调用方法
new fdj();
</script>
</body>
</html>

style.css

*{
margin: 0;
padding: 0;
}
.box {
width: 171px;
height: 256px;
padding: 5px;
border: 1px solid #ccc;
position: relative;
}

.box .smallBox {
width: 171px;
height: 256px;
background: #eee;
position: relative;
}

.box .floa {
width: 50px;
height: 50px;
border: 1px solid #000;
background: #fff;
filter: alpha(opacity: 30);
opacity: 0.3;
position: absolute;
top: 0;
left: 0;
display: none;
}

.box .mark {
width: 100%;
height: 100%;
position: absolute;
z-index: 2;
left: 0px;
top: 0px;
background: red;
filter: alpha(opacity: 0);
opacity: 0;
cursor: pointer;
}

.box .bigBox {
position: absolute;
top: -1px;
left: 215px;
width: 171px;
height: 256px;
overflow: hidden;
border: 2px solid #CCC;
display: none;
}

.box .bigBox img {
position: absolute;
top: -30px;
left: -80px;
}

fdj.js

//面向对象的方式写的。

function fdj(){
this.box=$(".box");
this.smallBox=$(".box .smallBox");
this.mark=$(".box .smallBox .mark");
this.floa=$(".box .smallBox .floa")
this.bigBox=$(".box .bigBox");
this.bigPic=$(".bigBox img");
this.init();
}

//每个函数都有一个prototype属性

fdj.prototype={
init:function(){
this.overMark();
this.outerMark();
this.moveMark();
},
overMark:function(){
this.mark.mouseover($.proxy(this.handleover,this));
},
handleover:function(){
this.floa.show();
this.bigBox.show();
},
outerMark:function(){
this.mark.mouseout($.proxy(this.handleouter,this));
},
handleouter:function(){
this.floa.hide();
this.bigBox.hide();
},
moveMark:function(){
this.mark.on("mousemove",$.proxy(this.handlemove,this));
},
handlemove:function(evt){
var left=evt.pageX-this.box.offset().left-this.mark.offset().left-this.floa.outerWidth()/2;
var top=evt.pageY-this.box.offset().top-this.mark.offset().top-this.floa.outerHeight()/2;

if(left<0){
left=0;
}
else if(left>this.mark.outerWidth()-this.floa.outerWidth()){
left=this.mark.outerWidth()-this.floa.outerWidth();
}
if(top<0){
top=0;
}else if(top>this.mark.outerHeight()-this.floa.outerHeight()){
top=this.mark.outerHeight()-this.floa.outerHeight();
}
this.floa.css({left:left,top:top});
var pX=left/(this.mark.outerWidth()-this.floa.outerWidth());
var pY=top/(this.mark.outerHeight()-this.floa.outerHeight());
this.bigPic.css({
left:-pX*(this.bigPic.outerWidth()-this.bigBox.outerWidth()),
top:-pY*(this.bigPic.outerHeight()-this.bigBox.outerHeight())
});

}
}

原文地址:https://www.cnblogs.com/wyryuebanwan/p/9788280.html

时间: 2024-08-26 00:29:27

JQ面向对象的放大镜的相关文章

面向对象的放大镜

//放大镜的面向对象,使用的时候需要获取放大镜的两个盒子,和滤镜还有放大后的图片对象;//最后用的时候要new 出mirror,然后传参进去;先获取://            var leftBox = $("#leftBox");//获取小图的盒子//            var rightBox = $("#rightBox");//获取大图的盒子//            var drag = $("#drag");//获取滤镜// v

jq放大镜效果

之前有在网上见过原生js实现放大镜的效果,但是代码量略多,今天和大家分享下我自己用jq实现的放大镜效果吧! 首先html布局结构和样式 贴上代码给大伙看一下: html结构: <div class="pic"> <img src="images/xiaotu.jpg" > <div class="big-pic"></div> </div> css样式: .pic{position:re

JQ选项卡(面向对象)

<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <meta h

JQ实战天猫淘宝放大镜

这个特效平时生活中很常见,话不多说先上效果图. 首先布局,遮罩层是这个效果中的重点精华,也就是下面代码中的shade. <style> * { margin:0px; padding:0px;} #box { width:400px; height:400px; border:5px solid #CCC; margin:100px auto; position:relative;} #box .simg {width:400px; height:400px; position:relativ

基于jq放大镜的原型扩展

传入参数图片源地址及放大倍数 //创建节点 $.fn.scale = function(json) {//json.src json.scale $(this).html(""); var width = $(this).width(); var height = $(this).height(); //创建图片 var img = $("<img>"); $(this).append(img); img.css({ "width":

JavaScript高级 面向对象(10)--onload与jq中ready的区别

说明(2017.4.2): 1. 在body中放一个img标签,src链接一张图片,那么页面会先读取html的document文档,然后再读取外部资源(这里没加onload其实就是从上往下顺序读取). 外部资源包括导入的js,css,图片,音乐,视频等等. onload会将所有的资源包括外部资源全部加载完成. 而jquery的ready只要加载完dom树就完成了,里面的图片等资源有没有加载无所谓. 1 <body> 2 <img src="1.jpg"> 3 &

基于jq, jquery.easie.js 开发面向对象通栏焦点图组件

<!--banner begin--> <div class="banner min_width" id="one" style=""> <span class="foucL fouc"></span> <ul> <li style="background: url(./images/banner.jpg) no-repeat center top;

JQ 放大镜

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; } #shangpin{ background-image: url(images/userimg.jpg); width: 300px; height: 300px; border: 1px red 

用面向对象解决 输入用户名自动显示邮箱后缀列表的方法

---恢复内容开始--- 当我们注册或者登录要用邮箱格式时会显示邮箱后缀的提示下拉框 效果如图所示 主要介绍了JS输入用户名自动显示邮箱后缀列表的方法,可实现自动显示邮箱后缀名列表的功能, 原理是:一个输入框 当我输入任何字的时候 自动下拉相应的邮箱提示,在输入框输入11的时候 下拉框有所有11的邮箱 输入其他的时候 有其他文案对应的邮箱. 同理 此插件不需要任何html标签,只需要一个输入框 有相对应的id类名就ok 且父级有个class类名,其他的都不需要.内部的HTML代码都是自动生成的