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對象中的每個元素
// 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決JQuery的ajax函數(shù)執(zhí)行失敗alert函數(shù)彈框一閃而過問題
這篇文章主要介紹了解決JQuery的ajax函數(shù)執(zhí)行失敗alert函數(shù)彈框一閃而過問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
jquery實(shí)現(xiàn)走馬燈特效實(shí)例(撲克牌切換效果)
本文主要介紹了jquery實(shí)現(xiàn)走馬燈特效實(shí)例(撲克牌切換效果),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
jQuery實(shí)現(xiàn)自定義checkbox和radio樣式
這篇文章主要介紹了jQuery實(shí)現(xiàn)自定義checkbox和radio樣式的相關(guān)資料,需要的朋友可以參考下2015-07-07
用jQuery實(shí)現(xiàn)圓點(diǎn)圖片輪播效果
在頁面的指定位置實(shí)現(xiàn)的圖片自動的左右輪流切換展示效果,當(dāng)點(diǎn)擊圖片左下的標(biāo)簽(或中間的小圓點(diǎn))切換到對應(yīng)的圖片。接下來通過本文給大家分享用jQuery實(shí)現(xiàn)圓點(diǎn)圖片輪播效果實(shí)例代碼,需要的朋友參考下2017-03-03
詳細(xì)介紹jQuery.outerWidth() 函數(shù)具體用法
這篇文章通過jQuery示例代碼演示outerWidth()函數(shù)的具體用法,介紹的非常詳細(xì),有需要的朋友可以借鑒2015-07-07
jquery dataTable 后臺加載數(shù)據(jù)并分頁實(shí)例代碼
本篇文章主要介紹了jquery dataTable 后臺加載數(shù)據(jù)并分頁實(shí)例代碼,具有一定的參考價值,有興趣的可以了解一下2017-06-06
Jquery選擇子控件"大于號"和" "區(qū)別介紹及使用示例
Jquery選擇子控件”>“:在給定的父元素下匹配所有的子元素;另一個就是在給定的祖先元素下匹配所有的后代元素,具體概述及使用示例如下,感興趣的朋友可以參考下哈2013-06-06

