這篇文章主要介紹如何使用Vue實現(xiàn)類似Spring官網(wǎng)圖片滑動效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!


可以看到, 隨著鼠標的滑動,綠色圖片和灰色圖片可以無縫的在鼠標倆兩邊切換顯示。
顯示這樣的效果其實很簡單,利用固定定位保證兩張圖片在同一位置下, 我們可以將灰色圖片當做背景層圖片,然后根據(jù)獲取到的實時X軸坐標, 動態(tài)改變綠色圖片的寬度, 隱藏超出X軸坐標的部分, 就可以達到這樣的效果, 簡單來說, 這效果就是動態(tài)改變上層圖片的寬度。
實現(xiàn)效果:

我這邊選擇了兩張同樣大小的KDA卡莎的圖片, 將金色圖作為背景圖,暗黑圖作為左側(cè)圖, 用了Vue的mousemove來獲取X軸坐標值, 并通過監(jiān)聽坐標軸變化來實時改變左側(cè)圖片的寬度。
鼠標部分, 簡化了Spring官網(wǎng)上鼠標位置出軸承的顯示, 采用了cursor: ew-resize樣式, 使得鼠標看起來可以左右滑動。
代碼粘貼
<template>
<div class="scroll">
<div class="container" @mousemove="mousemove">
<div class="base"></div>
<div class="left" ref="left">
<img src="../../static/image/kda-karsa.jpg" alt="">
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
posX: 0
}
},
methods: {
mousemove(e) {
// 獲取x 坐標
this.posX = e.offsetX
}
},
watch: {
posX(curX) {
this.$refs.left.style.width = `${curX}px`
}
}
}
</script>
<style lang="scss" scoped>
.scroll{
.container{
width: 960px;
height: 540px;
background-color: #cccccc;
position: relative;
cursor: ew-resize;
.base{
position: absolute;
width: 960px;
height: 540px;
top: 0;
left: 0;
background: url('../../static/image/kda-karsa-golden.jpg') no-repeat;
background-size: 100%;
}
.left{
position: absolute;
width: 480px;
height: 540px;
overflow: hidden;
top: 0;
left: 0;
img{
width: 960px;
height: 540px;
}
}
}
}
</style>以上是“如何使用Vue實現(xiàn)類似Spring官網(wǎng)圖片滑動效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱:如何使用Vue實現(xiàn)類似Spring官網(wǎng)圖片滑動效果-創(chuàng)新互聯(lián)
文章鏈接:http://www.chinadenli.net/article26/idocg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、全網(wǎng)營銷推廣、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容