jquery自定義放大鏡效果
本文實(shí)例為大家分享了jquery自定義放大鏡效果的具體代碼,供大家參考,具體內(nèi)容如下
jquery定義插件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/jquery3.6.0.js"></script>
<style type="text/css">
div{
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div></div>
<div></div>
<script>
// 1、jquery的插件,定義在jquery.fn的基礎(chǔ)上的
// 2、命名沖突的解決
// 3、循環(huán)jquery對(duì)象中的每個(gè)元素
// 4、在函數(shù)中,將jquery返回(this)
(function($){
$.fn.extend({
randomColor:function(){
// this指向的就是我們通過$選擇器選取到的所有元素組成的偽數(shù)組
function random(){
var r=Math.floor(Math.random()*256);
var g=Math.floor(Math.random()*256);
var b=Math.floor(Math.random()*256);
return 'rgb('+ r +','+ g +','+ b +')';
}
this.each(function(index){
$(this).css({
backgroundColor:random()
})
})
return this;
}
})
})(jQuery);
$('div').randomColor();
</script>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.magnifier .left{
width: 240px;
height: 150px;
float: left;
position: relative;
}
.magnifier .left img{
width: 240px;
height: 150px;
}
.magnifier .left .mask{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
background-color: black;
opacity: 0.4;
}
.magnifier .float{
width: 50px;
height: 50px;
background: hotpink;
opacity: 0.5;
position: absolute;
left: 0;
top: 0;
}
.magnifier .right{
height: 200px;
width: 200px;
background-image: url(img/2.jpg) ;
float: left;
margin-left: 50px;
}
</style>
</head>
<body>
<div class="magnifier">
<div class="left">
<img src="./img/2.jpg" >
<div class="float">
</div>
<div class="mask"></div>
</div>
<div class="right"></div>
</div>
<script src="js/jquery3.6.0.js"></script>
<script>
(function($){
$.fn.extend({
magnifier:function(){
this.each(function(){
var that=this;
$('.left',this).mousemove(function (evt){
var x=evt.offsetX;
var y=evt.offsetY;
var float=$('.float',this);
x=x-float.width/2;
y=y-float.height/2;
if(x<0){
x=0;
}
if(y<0){
y=0;
}
if(x > $(this).width()-float.width()){
x = $(this).width()-float.width();
}
if(y > $(this).height()-float.height()){
y = $(this).height()-float.height();
}
float.css({
left:x,
top:y
});
$('.right',that).css({
backgroundPosition:x*-4+'px' + y*-4+'px'
})
}).mouseover(function(){
}).mouseout(function(){
})
});
}
})
})(jQuery)
$('.magnifier').magnifier();
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery實(shí)現(xiàn)表格行上下移動(dòng)和置頂效果
本文給大家分享的是一款由jQuery實(shí)現(xiàn)的表格行上下移動(dòng)以及置頂效果的代碼,非常的簡(jiǎn)單實(shí)用,這里給出了核心代碼,有需要的小伙伴可以參考下。2015-06-06
jquery通過closest選擇器修改上級(jí)元素的方法
這篇文章主要介紹了jquery通過closest選擇器修改上級(jí)元素的方法,實(shí)例分析了jQuery中closest選擇器的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
jQuery-ui引入后Vs2008的無智能提示問題解決方法
引入jQuery-vsdoc文件后,jQuery庫(kù)就能智能提示了,解決方法很簡(jiǎn)單在jQuery-ui的目錄下再加入一個(gè)空的JS文件,命名jquery-ui-vsdoc.js2014-02-02
jQuery實(shí)現(xiàn)的記住帳號(hào)密碼功能完整示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的記住帳號(hào)密碼功能,結(jié)合完整實(shí)例形式分析了jQuery使用jquery.cookie.js插件記錄用戶信息相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
jQuery學(xué)習(xí)總結(jié)之元素的相對(duì)定位和選擇器(持續(xù)更新)
jQuery中不僅可以使用選擇器就行絕對(duì)定位,而且還可以進(jìn)行相對(duì)定位,只要在$()中指定第二個(gè)參數(shù),第二個(gè)參數(shù)就是相對(duì)的元素。第二個(gè)參數(shù)傳遞一個(gè)jQuery對(duì)象,則相對(duì)于這個(gè)對(duì)象為基準(zhǔn)進(jìn)行相對(duì)的選擇。2011-04-04
jQuery easyui datagird編輯行刪除行功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了jQuery easyui datagird編輯行刪除行功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-09-09

