這篇文章給大家分享的是有關JavaScript實現淘寶放大鏡的方法有哪些的內容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
創(chuàng)新互聯公司專注于商水企業(yè)網站建設,自適應網站建設,商城網站建設。商水網站建設公司,為商水等地區(qū)提供建站服務。全流程按需網站建設,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯公司專業(yè)和態(tài)度為您提供的服務這個,當你的鼠標移動到左邊的主圖上時,右邊會出現一個放大的圖,暫且就把這個叫做放大鏡吧。
大概的做法第一種,左邊一個小圖,右邊一個原圖,當鼠標在小圖上移動的時候,通過更改left和top的值來實現同步移動(原圖的position屬性設置為absolute)
第二種,鼠標在小圖上移動的時候,通過改變原圖的background-position的值來同步移動。
第一步,獲取鼠標在小圖上的位置,并且定位好跟隨鼠標的方塊(你應該知道是哪個方塊吧。。)的位置。
//e.offsetX ,offsetY是鼠標的位置
//方塊的left top在你的鼠標的左上方(網頁左上角是原點),因此是減去一個方塊的一半。
var x = e.offsetX - 方塊.offsetWidth / 2;
var y = e.offsetY - 方塊.offsetHeight / 2;
方塊.style.left = x + 'px';
方塊.style.top = y + 'px';這明顯是不足夠的!
還需要考慮極端位置/情況
如果只用上面的設置,那么當你的鼠標移動到圖片邊緣的時候,方塊有一半會出現在圖片外。

正確的應該是當你的方塊觸碰到邊緣的時候,你的方塊就不能在移動了,盡管你的鼠標還在往下圖中“鼠標的有效活動區(qū)域”外移動。

那么得加點代碼
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (x > 小圖.offsetWidth - 方塊.offsetWidth) {
x = 小圖.offsetWidth - 方塊.offsetWidth;
}
if (y > 小圖.offsetHeight - 方塊.offsetHeight) {
y = 小圖.offsetHeight - 方塊.offsetHeight;
}第二步,控制大圖里的left - top或者background-position
//第一種方法:需要注意的是這里的left 和 top得反過來,你鼠標在小圖上往下移的時候,對應的大圖其實是往上移的。
//所以:大圖上的left = -小圖上的left * 他們的縮放倍率
大圖.style.display = "block";
大圖.style.left = -x * 大圖.offsetWidth / 小圖.offsetWidth + 'px';
大圖.style.top = -y * 大圖.offsetHeight / 小圖.offsetHeight + 'px';
//第二種方法,這里需要注意 backgroundPosition的值是從0 - 100%的(得用百分比表示);
//需要注意的是何時為百分百,從上面的極端情況判定我們可以知道
//x 是從0 到 mask.offsetWidth - rect.offsetWidth;
//因此這就是0 - 100%;y同理
大圖.style.display = "block";
大圖.style.backgroundPosition =`${x/(mask.offsetWidth - rect.offsetWidth)*100}% ${y/(mask.offsetHeight- rect.offsetHeight)*100}%`;注意事項我們上面說在小圖img上綁定mousemove事件來定位方塊,其實實際操作上,我們不能直接用img來綁定,而是得用一個和img一樣大小遮罩層來綁定,不然在你鼠標移動的時候,圖片會瘋狂閃爍,瘋狂!crazy!
還有 就是函數節(jié)流,這個想節(jié)流就節(jié)流吧。
還有個很重要的,就是右邊那個顯示大圖的p的大小,一定得是小圖上的方塊大小 * 縮放倍率的大小,如果過大,則會多出空白,過小,顯示不完全。下面有代碼,你可以帶回家瘋狂測試。
我知道我可能說的不是很詳細,所以。。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>tb放大鏡</title>
<style>
.small-box {
position: relative;
height: 300px;
}
.small-pic {
width: auto;
height: 300px;
}
.mask {
width: 526px;
position: absolute;
top: 0;
left: 0;
z-index: 1;
height: 100%;
cursor: crosshair;
}
.rect {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
opacity: .5;
background-color: red;
z-index: 0;
}
.big-box {
display: inline-block;
position: relative;
width: 266px;
height:266px;
border: 1px solid red;
overflow: hidden;
}
.big-pic {
position: absolute;
width: 1400px;
height: 798px;
top: 0;
left: 0;
}
.big-pic2{
display: inline-block;
width: 266px;
height:266px;
background-size: auto 798px;
background-image: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1550846791000&di=15edaf7bce643e13b65f70c82d74de30&imgtype=0&src=http%3A%2F%2Fpic.92to.com%2F360%2F201604%2F08%2F19864861_13.jpg");
background-position: 0 0;
}
</style>
</head>
<body>
<div>
<img
src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1550846791000&di=15edaf7bce643e13b65f70c82d74de30&imgtype=0&src=http%3A%2F%2Fpic.92to.com%2F360%2F201604%2F08%2F19864861_13.jpg"
alt="">
<div></div>
<div style="display: none"></div>
</div>
<div>
<img
style="display: none"
src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1550846791000&di=15edaf7bce643e13b65f70c82d74de30&imgtype=0&src=http%3A%2F%2Fpic.92to.com%2F360%2F201604%2F08%2F19864861_13.jpg"
alt="">
</div>
<div>
<div></div>
</div>
</body>
<script>
window.onload = function () {
var mask = document.getElementsByClassName('mask')[0];
//為什么要用mask呢?不直接用選中small-pic。
//如果直接選擇圖片標簽來綁定下面的mouseover事件,圖片會一直閃爍!所以我們得給他一個和圖片一樣大小的遮罩層
var rect = document.getElementsByClassName('rect')[0];
var bPic = document.getElementsByClassName("big-pic")[0];
var bPic2 = document.getElementsByClassName("big-pic2")[0];
mask.addEventListener('mousemove', throttle(magnifier,100))
function magnifier(e){
//方塊的left top在你的鼠標的左上方(網頁左上角是原點),因此是減去一個方塊的一半。
var x = e.offsetX - rect.offsetWidth / 2;
var y = e.offsetY - rect.offsetHeight / 2;
//極端情況,也就是當你的鼠標上的方塊到四個邊的邊緣的時候。
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (x > mask.offsetWidth - rect.offsetWidth) {
x = mask.offsetWidth - rect.offsetWidth;
}
if (y > mask.offsetHeight - rect.offsetHeight) {
y = mask.offsetHeight - rect.offsetHeight;
}
//方塊定位
rect.style.display="block";
rect.style.left = x + 'px';
rect.style.top = y + 'px';
//第一種方法:需要注意的是這里的left 和 top得反過來,你鼠標在小圖上往下移的時候,對應的大圖其實是往上移的。
//所以:大圖上的left = -小圖上的left * 他們的縮放倍率
bPic.style.display = "block";
bPic.style.left = -x * bPic.offsetWidth / mask.offsetWidth + 'px';
bPic.style.top = -y * bPic.offsetHeight / mask.offsetHeight + 'px';
//第二種方法,這里需要注意 backgroundPosition的值是從0 - 100%的(得用百分比表示);
//需要注意的是何時為百分百,從上面的極端情況判定我們可以知道
//x 是從0 到 mask.offsetWidth - rect.offsetWidth;
//因此這就是0 - 100%;y同理
bPic2.style.display = "block";
bPic2.style.backgroundPosition =`${x/(mask.offsetWidth - rect.offsetWidth)*100}% ${y/(mask.offsetHeight- rect.offsetHeight)*100}%`;
}
mask.addEventListener('mouseout',function(){
rect.style.display = "none";
bPic.style.display = "none";
bPic2.style.display = "none";
})
//函數節(jié)流
function throttle(fn, delay) {
var pre = new Date().getTime();
return function () {
var context = this;
var args = arguments;
var now = new Date().getTime();
if (now - pre > delay) {
fn.apply(this,arguments);
}
}
}
}
</script>
</html>感謝各位的閱讀!關于JavaScript實現淘寶放大鏡的方法有哪些就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
新聞名稱:JavaScript實現淘寶放大鏡的方法有哪些-創(chuàng)新互聯
本文URL:http://www.chinadenli.net/article36/dhedsg.html
成都網站建設公司_創(chuàng)新互聯,為您提供微信公眾號、Google、網站改版、品牌網站建設、全網營銷推廣、云服務器
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯