如下圖,想要做一個鼠標在圖片滑動,網(wǎng)狀框跟隨鼠標一起移動。遇到了很奇怪的問題,圖1是正常的樣子,但是當(dāng)我鼠標再次移動的時候,就變成了圖2。如果連續(xù)移動的話,網(wǎng)狀框就會閃動,說白了就是第1秒網(wǎng)狀框會跟著鼠標走,但是下一秒網(wǎng)狀框就會回到左上角。如圖3
圖1
圖2
網(wǎng)狀框和商品圖片都在一個div里,父級有的相對定位,網(wǎng)狀框子級用的絕對定位。而且就此我還打印了網(wǎng)狀框的left值,如圖3
圖3
下面是代碼
模板
<div
class="goods_description_pic"
@mouseenter="showcheckeddetailelement=true"
@mouseleave="showcheckeddetailelement=false"
@mousemove="checkeddetailproduct($event)">
<img :src="productinformation.productimg">
<span
v-show="showcheckeddetailelement"
@mouseenter="showcheckeddetailelement=true"
class="goods_description_detailed_see"
:style="{ left: followcheckedx+'px', top: followcheckedy+'px'}"></span>
</div>
js
export default{
data(){
return {
followcheckedx: 0,
followcheckedy: 0
}
},
methods: {
checkeddetailproduct (e){
// offsetX是鼠標相對于窗口的距離
// e.clientX - e.offsetX 標簽距瀏覽器左端的距離
this.followcheckedx = e.offsetX - 75;
this.followcheckedy = e.offsetY - 75;
if(this.followcheckedx>=150){
this.followcheckedx=150;
}
if(this.followcheckedy>=150){
this.followcheckedy=150;
}
if(this.followcheckedx<0){
this.followcheckedx=0;
}
if(this.followcheckedy<0){
this.followcheckedy=0;
}
console.log('left:' + this.followcheckedx)
}
}
}
肯定是有哪個地方疏忽了,謝謝大家?guī)臀铱匆幌拢蹅円黄饘W(xué)習(xí)進步
主要的問題已經(jīng)找到了,如果在最外層的div上面加上mousemove事件,那么就相當(dāng)于在img和span上分別加了mousemove事件,他們就會根據(jù)鼠標在自己的元素上進行重新定位,從而導(dǎo)致了第一秒在這里,下一秒又在另一個地方的情況。
問題已經(jīng)解決,謝謝各位大神幫忙,我會嘗試另外幾種方法
offsetX,offsetY是鼠標相對于事件源元素的X,Y坐標
(事件源:當(dāng)前操作的那個元素就是事件源)
而此時在div中的還有img和span,都會成為事件源,它就GG了,不知道以哪個為參考。
怎么辦~~!把@mousemove事件改為@mousemove.self,再把img刪掉(此時鼠標事件只針對div,不刪掉的話,鼠標移到img上不會觸發(fā)div的鼠標事件),然后就會發(fā)現(xiàn)“正常”了
但是!這也是有缺陷的,當(dāng)鼠標在遮罩上小幅度移動的時候,遮罩并不會跟著走,因為span(遮罩)也會阻止鼠標事件的觸發(fā)!(大幅度移動的時候鼠標接觸div,span才會跟過去)
所以~鼠標跟隨移動還是使用下面這種方法吧,給你寫了例子,僅供參考,邊緣判斷還需要你自己寫一下哦,
<div class='box'
ref='box'
@mousemove="handleMousemove">
<img src="xxx" />
<span class='mask'
:style="{left: isLeft, top: isTop}"></span>
</div>
handleMousemove() {
// 圖片離body的距離
const boxL = this.$refs.box.offsetLeft
const boxT = this.$refs.box.offsetTop
// 75為半透明遮罩高度(寬度)的一半(假設(shè)它為正方形)
this.isLeft = event.clientX - boxL - 75 + 'px'
this.isTop = event.clientY - boxT - 75 + 'px'
}
另外,希望你能知其然也知其所以然~(* ̄︶ ̄)
修改后的表述有問題。mousemove 是冒泡的,所以相當(dāng)于接收到不同 target 發(fā)送來的事件,所以當(dāng)你使用 offsetX offsetY 這種跟元素相關(guān)的屬性,定位就會變化。于是,浮層就跑掉了,然后鼠標又回到原始圖片上面,定位恢復(fù),浮層又回來。如此反復(fù)。
解決方案有兩個:
MouseEvent.x 這種元素?zé)o關(guān)的屬性,配合 div.getBoundingClientRect() 計算位置<span>,方法參考下面span {
pointer-events: none;
}
關(guān)于 pointer-events。
問題已經(jīng)解決,我的思路是再單拉出來一個div,寬度和高度都與圖片窗口div一樣,鼠標移動事件在單拉出來的div上設(shè)置。代碼如下
模板
<div
class="goods_description_pic"
@mouseenter="showcheckeddetailelement=true"
@mouseleave="showcheckeddetailelement=false">
<img
class="productimg"
:src="productinformation.productimg">
<span
v-show="showcheckeddetailelement"
@mouseenter="showcheckeddetailelement=true"
class="goods_description_detailed_see"
:style="{ left: followcheckedx+'px', top: followcheckedy+'px'}"></span>
<div
class="detial_see_wrap"
@mousemove="checkeddetailproduct($event)">
</div>
</div>
js
methods: {
checkeddetailproduct (e){
this.followcheckedx = e.offsetX -75;
this.followcheckedy = e.offsetY - 75;
/* 邊緣判斷*/
if(this.followcheckedx>=150){
this.followcheckedx=150;
}
if(this.followcheckedy>=150){
this.followcheckedy=150;
}
if(this.followcheckedx<0){
this.followcheckedx=0;
}
if(this.followcheckedy<0){
this.followcheckedy=0;
}
}
}北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復(fù)興的升級產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。