var?x,y,pos;
$('#zoom_preview').mousemove(function(e){
????x?=?Math.round(e.offsetX);
????y?=?Math.round(e.offsetY);
????document.getElementById('zoom_window').style.left?=?x?+?"px";
????document.getElementById('zoom_window').style.top?=?y?+?"px";
$('.test').html("x="+x+"?y="+y);
});<div?id="zoom_preview"?style="width:360px;height:360px;position:?relative;">
????<div?id="zoom_window"?style="width:?50%;height:?50%;background-color:lightblue;opacity:?0.6;position:?absolute;"></div>
</div>
<div?class="test"></div>這段代碼是嘗試寫放大鏡功能的,如果不設置zoom_window的定位屬性left和top的話還可以正確獲取到e返回的offset,一旦設置了zoom_window的定位屬性,頁面效果就是zoom_window不能正確被設置成e返回的offset,隨機性地會彈到錯誤位置口頭描述不清楚,我再追加個渣圖展示一下這個鬼畜
使用js動態設置元素定位屬性為鼠標坐標時元素會間歇性抖動
幻藍風暴
2016-10-29 10:04:22