如下圖,想要做一個鼠標在圖片滑動,網(wǎng)狀框跟隨鼠標一起移動。遇到了很奇怪的問題,圖1是正常的樣子,但是當我鼠標再次移動的時候,就變成了圖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ī)臀铱匆幌?,咱們一起學習進步
主要的問題已經(jīng)找到了,如果在最外層的div上面加上mousemove事件,那么就相當于在img和span上分別加了mousemove事件,他們就會根據(jù)鼠標在自己的元素上進行重新定位,從而導致了第一秒在這里,下一秒又在另一個地方的情況。
問題已經(jīng)解決,謝謝各位大神幫忙,我會嘗試另外幾種方法
offsetX
,offsetY
是鼠標相對于事件源元素
的X,Y坐標
(事件源:當前操作的那個元素就是事件源)
而此時在div
中的還有img
和span
,都會成為事件源,它就GG了,不知道以哪個為參考。
怎么辦~~!把@mousemove
事件改為@mousemove.self
,再把img
刪掉(此時鼠標事件只針對div,不刪掉的話,鼠標移到img上不會觸發(fā)div的鼠標事件),然后就會發(fā)現(xiàn)“正?!绷?/p>
但是!這也是有缺陷的,當鼠標在遮罩上小幅度移動的時候,遮罩并不會跟著走,因為span(遮罩)也會阻止鼠標事件的觸發(fā)?。ù蠓纫苿拥臅r候鼠標接觸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為半透明遮罩高度(寬度)的一半(假設它為正方形)
this.isLeft = event.clientX - boxL - 75 + 'px'
this.isTop = event.clientY - boxT - 75 + 'px'
}
另外,希望你能知其然也知其所以然~(* ̄︶ ̄)
修改后的表述有問題。mousemove
是冒泡的,所以相當于接收到不同 target
發(fā)送來的事件,所以當你使用 offsetX
offsetY
這種跟元素相關的屬性,定位就會變化。于是,浮層就跑掉了,然后鼠標又回到原始圖片上面,定位恢復,浮層又回來。如此反復。
解決方案有兩個:
MouseEvent.x
這種元素無關的屬性,配合 div.getBoundingClientRect()
計算位置<span>
,方法參考下面span {
pointer-events: none;
}
關于 pointer-events。
問題已經(jīng)解決,我的思路是再單拉出來一個div,寬度和高度都與圖片窗口div一樣,鼠標移動事件在單拉出來的div上設置。代碼如下
模板
<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年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學院和江蘇省首批服務外包人才培訓基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術與教育服務機構,發(fā)展為教育服務業(yè)的綜合性企業(yè)集團,成為集合面授教學培訓、網(wǎng)
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經(jīng)驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。