小程序click-scroll組件設(shè)計(jì)
一. 背景
有些業(yè)務(wù)需求,要求前端展示的內(nèi)容多時(shí)可以通過(guò)scroll的形式拖拉查看,但是太多的滾動(dòng)條又造成頁(yè)面太亂,于是封裝了這個(gè)click-scroll 組件。在組件上設(shè)定好展示的位置和空間大小,在組件內(nèi)部放置實(shí)際要展示的內(nèi)容,實(shí)際展示的內(nèi)容寬度或長(zhǎng)或短都由此組件來(lái)控制。
二. 功能
組件內(nèi)的內(nèi)容寬度超過(guò)組件寬度時(shí),組件兩側(cè)會(huì)自動(dòng)出現(xiàn)『左右移動(dòng)』交互。
當(dāng)內(nèi)部展示的內(nèi)容超過(guò)組件的可見(jiàn)區(qū)域時(shí),可以在組件的可見(jiàn)區(qū)域單擊拖動(dòng)查看內(nèi)容
三. 背景知識(shí),元素大小的測(cè)量
1.偏移量(offset dimension):
元素在屏幕上占用的可見(jiàn)的所有空間,元素的可見(jiàn)大小由其高度、寬度決定,包括所有內(nèi)邊距、滾動(dòng)條和邊框大小。由四個(gè)值決定:offsetHeight、offsetWidth、offsetLeft和offsetRight。
- offsetHeight:元素在垂直方向上占用的空間大小,以像素計(jì)。包括元素的高度、(可見(jiàn))水平滾動(dòng)條的高度、上邊框高度和下邊框高度。
- offsetWidth:元素在水平方向上占用的空間大小,以像素計(jì)。包括元素的寬度、(可見(jiàn))垂直滾動(dòng)條的寬度、左邊框?qū)挾群陀疫吙驅(qū)挾取?/li>
- offsetLeft:元素的左外邊框至包含元素的左內(nèi)邊框之間的像素距離。 d.
- offsetTop:元素的上外邊框至包含元素的上內(nèi)邊框之間的像素距離。

2.客戶(hù)區(qū)大小(client dimension)
元素內(nèi)容及其內(nèi)邊距所占據(jù)空間的大小,滾動(dòng)條占用的空間不計(jì)算在內(nèi)。
- clientWidth:元素內(nèi)容區(qū)寬度加上左右內(nèi)邊距的寬度
- clientHeight: 元素內(nèi)容區(qū)高度加上上下內(nèi)邊距的高度

3.滾動(dòng)大小(scroll dimension)
包含滾動(dòng)內(nèi)容的元素的大小。
- scrollHeight:在沒(méi)有滾動(dòng)條的情況下,元素內(nèi)容的實(shí)際總高度。
- scrollWidth:在沒(méi)有滾動(dòng)條的情況下,元素內(nèi)容的實(shí)際總寬度。
- scrollLeft:被隱藏在內(nèi)容區(qū)域左側(cè)的像素?cái)?shù)。通過(guò)設(shè)置這個(gè)屬性可以改變?cè)氐臐L動(dòng)位置。
- scrollTop:被隱藏在內(nèi)容區(qū)域上方的像素?cái)?shù)。通過(guò)設(shè)置這個(gè)屬性可以改變?cè)氐臐L動(dòng)位置。

四. 組件設(shè)計(jì)思路

五. 使用文檔
slot:
| 參數(shù) | 說(shuō)明 | 類(lèi)型 |
|---|---|---|
| content | 組件實(shí)際要展示的內(nèi)容 | dom |
<click-scroll>
<template slot="content">
<div>
我是實(shí)際要展示的內(nèi)容啊啊啊啊啊……
</div>
</template>
</click-scroll>
六. 組件源碼
<template>
<div class="hui-hui" :id="domId.compID">
<!--向左滑動(dòng)-->
<div class="hui-drag-left"
:class="{'hui-drag-action': drag.isLeft}"
v-if="drag.isShow"
@click="onClickLeft">
</div>
<!--展示的內(nèi)容-->
<div :id="domId.containerID"
class="hui-container"
v-show="hasContent"
ref='container'
@mousedown="onMouseDown">
<slot name="content"></slot>
</div>
<div v-show="!hasContent" class="hui-no-data">暫無(wú)數(shù)據(jù)</div>
<!--向右滑動(dòng)-->
<div class="hui-drag-right"
:class="{'hui-drag-action': drag.isRight}"
v-if="drag.isShow"
@click="onClickRight">
</div>
</div>
</template>
<script>
import store from '@/store'
export default {
name: 'cards-container',
data () {
return {
hasContent: false,
domId: {
compID: `hui-comp-${+new Date()}`,
containerID: `hui-container-${+new Date()}`
},
drag: {
isShow: false,
isLeft: false,
isRight: false
}
}
},
methods: {
judgeHasContent () {
this.hasContent = this.$slots.hasOwnProperty('content')
},
judgeDragIsShow () {
const compWidth = this.getCompWidth()
const contextMaxWidth = this.getContextMaxWidth()
if (compWidth >= contextMaxWidth) {
this.drag.isShow = false
} else {
this.drag.isShow = true
}
return this.drag.isShow
},
judgeIsLeft () {
const containerDom = this.getContainerDom()
const contentWidth = this.getContextMaxWidth()
if (!containerDom && !contentWidth) this.drag.isLeft = false
if (containerDom.offsetWidth + containerDom.scrollLeft >= contentWidth) {
this.drag.isLeft = false
} else {
this.drag.isLeft = true
}
},
judgeIsRight () {
const containerDom = this.getContainerDom()
if (!containerDom) this.drag.isRight = false
if (containerDom.scrollLeft === 0) {
this.drag.isRight = false
} else {
this.drag.isRight = true
}
},
getCompDom () {
return document.getElementById(this.domId.compID) || null
},
getCompWidth () {
const compDom = this.getCompDom()
if (!compDom) {
return 0
} else {
return compDom.offsetWidth
}
},
getContainerDom () {
return document.getElementById(this.domId.containerID) || null
},
getContextMaxWidth () {
if (!this.$refs.hasOwnProperty('container')) {
return 0
} else {
const widthArr = []
for(let e of this.$refs['container'].childNodes) {
widthArr.push(e.offsetWidth)
}
return Math.max(...widthArr)
}
},
onMouseDown (e) { // 手動(dòng)滑動(dòng)
const containerDom = this.getContainerDom()
if (!containerDom) return
let scrollLeft = containerDom.scrollLeft
const containerLeft = containerDom.offsetLeft
let ev = e || window.event
let offsetLeft = ev.clientX - containerLeft
document.onmousemove = (e) => {
let ev = e || window.event
let nowOffsetLeft = ev.clientX - containerLeft
containerDom.scrollLeft = scrollLeft + (offsetLeft - nowOffsetLeft)
this.judgeIsLeft()
this.judgeIsRight()
}
document.onmouseup = () => {
document.onmousemove = null
document.onmouseup = null
}
},
onClickLeft () { // 向左滑動(dòng)
if (!this.hasContent && !this.drag.isLeft) return
const containerDom = this.getContainerDom()
if (!containerDom) return
const scrollWidth = containerDom.offsetWidth
containerDom.scrollLeft += scrollWidth
this.judgeIsLeft()
this.judgeIsRight()
},
onClickRight () { // 向右滑動(dòng)
if (!this.hasContent && !this.drag.isRight) return
const containerDom = this.getContainerDom()
if (!containerDom) return
const scrollWidth = containerDom.offsetWidth
containerDom.scrollLeft -= scrollWidth
this.judgeIsLeft()
this.judgeIsRight()
}
},
updated () {
this.$nextTick(() => {
this.judgeHasContent()
const isShow = this.judgeDragIsShow()
if (isShow) {
this.judgeIsLeft()
this.judgeIsRight()
}
})
},
mounted () {
this.judgeHasContent()
const isShow = this.judgeDragIsShow()
if (isShow) {
this.judgeIsLeft()
this.judgeIsRight()
}
}
}
</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript中通過(guò)arguments參數(shù)偽裝方法重載
面向?qū)ο蟮母呒?jí)語(yǔ)言中,都有方法的重載,在js中可以通過(guò)arguments這個(gè)參數(shù)來(lái)偽裝成函數(shù)重載,具體如下2014-10-10
基于JS+Canves實(shí)現(xiàn)點(diǎn)擊按鈕水波紋效果
本文給大家分享基于js和canves實(shí)現(xiàn)點(diǎn)擊按鈕水波紋效果,效果非常逼真,對(duì)此感興趣的朋友一起看看吧2016-09-09
用js判斷是否為360瀏覽器的實(shí)現(xiàn)代碼
這篇文章主要介紹了用js判斷是否為360瀏覽器的實(shí)現(xiàn)代碼,有時(shí)候我們需要判斷是否為360瀏覽器,包括百度聯(lián)盟后臺(tái)就有這樣的提示需要的朋友可以參考下2015-01-01
JS驗(yàn)證 只能輸入小數(shù)點(diǎn),數(shù)字,負(fù)數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇JS驗(yàn)證 只能輸入小數(shù)點(diǎn),數(shù)字,負(fù)數(shù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
兼容IE/Firefox/Opera/Safari的檢測(cè)頁(yè)面裝載完畢的腳本Ext.onReady的實(shí)現(xiàn)
其中對(duì)于IE的檢測(cè)很有意思。 以上代碼,整理自Extjs的腳本,完全可以代替 Ext.onReady使用。2009-07-07
JavaScript給每一個(gè)li節(jié)點(diǎn)綁定點(diǎn)擊事件的實(shí)現(xiàn)方法
這篇文章主要介紹了JavaScript給每一個(gè)li節(jié)點(diǎn)綁定點(diǎn)擊事件的實(shí)現(xiàn)方法,包括js循環(huán)給li綁定參數(shù)不同的點(diǎn)擊事件,需要的朋友可以參考下2016-12-12
JS使用oumousemove和oumouseout動(dòng)態(tài)改變圖片顯示的方法
這篇文章主要介紹了JS使用oumousemove和oumouseout動(dòng)態(tài)改變圖片顯示的方法,涉及javascript鼠標(biāo)事件及圖片操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

