第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在vue中關(guān)于鼠標滑動事件

在vue中關(guān)于鼠標滑動事件

守候你守候我 2019-03-14 14:15:16
如下圖,想要做一個鼠標在圖片滑動,網(wǎng)狀框跟隨鼠標一起移動。遇到了很奇怪的問題,圖1是正常的樣子,但是當(dāng)我鼠標再次移動的時候,就變成了圖2。如果連續(xù)移動的話,網(wǎng)狀框就會閃動,說白了就是第1秒網(wǎng)狀框會跟著鼠標走,但是下一秒網(wǎng)狀框就會回到左上角。如圖3網(wǎng)狀框和商品圖片都在一個div里,父級有的相對定位,網(wǎng)狀框子級用的絕對定位。而且就此我還打印了網(wǎng)狀框的left值,如圖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>jsexport 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ī)臀铱匆幌?,咱們一起學(xué)習(xí)進步主要的問題已經(jīng)找到了,如果在最外層的div上面加上mousemove事件,那么就相當(dāng)于在img和span上分別加了mousemove事件,他們就會根據(jù)鼠標在自己的元素上進行重新定位,從而導(dǎo)致了第一秒在這里,下一秒又在另一個地方的情況。問題已經(jīng)解決,謝謝各位大神幫忙,我會嘗試另外幾種方法
查看完整描述

4 回答

?
蕭十郎

TA貢獻1815條經(jīng)驗 獲得超13個贊

e.offsetX 確定是相對于窗口的距離么?
應(yīng)該是相對于鼠標位置元素上層父級定位為相對或絕對的元素距離 沒找到就相對于body
感覺你這offsetX在 相對于網(wǎng)狀框和相對于div盒子來回切換了 所以在變

查看完整回答
反對 回復(fù) 2019-03-29
?
森林海

TA貢獻2011條經(jīng)驗 獲得超2個贊

offsetX,offsetY是鼠標相對于事件源元素的X,Y坐標

(事件源:當(dāng)前操作的那個元素就是事件源)


而此時在div中的還有img和span,都會成為事件源,它就GG了,不知道以哪個為參考。


怎么辦~~!把@mousemove事件改為@mousemove.self,再把img刪掉(此時鼠標事件只針對div,不刪掉的話,鼠標移到img上不會觸發(fā)div的鼠標事件),然后就會發(fā)現(xiàn)“正?!绷?/p>


但是!這也是有缺陷的,當(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'

    }

另外,希望你能知其然也知其所以然~(* ̄︶ ̄)


查看完整回答
反對 回復(fù) 2019-03-29
?
肥皂起泡泡

TA貢獻1829條經(jīng)驗 獲得超6個贊

修改后的表述有問題。mousemove 是冒泡的,所以相當(dāng)于接收到不同 target 發(fā)送來的事件,所以當(dāng)你使用 offsetX offsetY 這種跟元素相關(guān)的屬性,定位就會變化。于是,浮層就跑掉了,然后鼠標又回到原始圖片上面,定位恢復(fù),浮層又回來。如此反復(fù)。


解決方案有兩個:


使用 MouseEvent.x 這種元素?zé)o關(guān)的屬性,配合 div.getBoundingClientRect() 計算位置

禁掉不想觸發(fā)事件的元素,比如 <span>,方法參考下面

span {

  pointer-events: none;

}

關(guān)于 pointer-events。

查看完整回答
反對 回復(fù) 2019-03-29
?
青春有我

TA貢獻1784條經(jīng)驗 獲得超8個贊

問題已經(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;

        }

    }

}


查看完整回答
反對 回復(fù) 2019-03-29
  • 4 回答
  • 0 關(guān)注
  • 1783 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號