DOM操作函数

包含对Vue节点(Virtual-DOM)的操作方法

scrollToElement

scrollToElement(node, options) 让页面滚动到那个对应的节点

这个API只能在 <scroller><list> 组件中用。

node参数说明:

参数 类型 说明
node Dom Node 要滚动到的那个节点,通过ref标记

options参数说明:

滚动时的偏移量和动画设置,格式为 Json Object

参数 类型 说明
offset number 一个到其可见位置的偏移距离,默认是 0
animated boolean 是否需要附带滚动动画,默认是true

scrollToElement示例

<template>
    <div class="wrapper">
        <scroller class="scroller">
            <div class="row" v-for="(name, index) in rows" :ref="'item'+index">
            <text class="text" :ref="'text'+index">{{name}}</text>
            </div>
        </scroller>
        <div class="group">
            <text @click="goto10" class="button">Go to 10</text>
            <text @click="goto20" class="button">Go to 20</text>
        </div>
    </div>
</template>
<script>
import umsApi from 'ums-api'
export default {
    data () {
        return {
            rows: []
        }
    },
    created () {
        for (let i = 0; i < 30; i++) {
            this.rows.push('row ' + i)
        }
    },
    methods: {
        goto10 (count) {
            const el = this.$refs.item10[0]
            umsApi.dom.scrollToElement(el, {})
        },
        goto20 (count) {
            const el = this.$refs.item20[0]
            umsApi.dom.scrollToElement(el, { offset: 0 })
        }
    }
}
</script>
<style scoped>
.scroller {
    width: 700px;
    height: 700px;
    border-width: 3px;
    border-style: solid;
    border-color: rgb(162, 217, 192);
    margin-left: 25px;
}
.row {
    height: 100px;
    flex-direction: column;
    justify-content: center;
    padding-left: 30px;
    border-bottom-width: 2px;
    border-bottom-style: solid;
    border-bottom-color: #DDDDDD;
}
.text {
    font-size: 45px;
    color: #666666;
}
.group {
    flex-direction: row;
    /*justify-content: space-around;*/
    justify-content: center;
    margin-top: 60px;
}
.button {
    width: 200px;
    padding-top: 20px;
    padding-bottom: 20px;
    font-size: 40px;
    margin-left: 30px;
    margin-right: 30px;
    text-align: center;
    color: #41B883;
    border-width: 2px;
    border-style: solid;
    border-color: rgb(162, 217, 192);
    background-color: rgba(162, 217, 192, 0.2);
}
</style>

getComponentRect

getComponentRect(ref, callback) 通过标签的 ref 获得其布局信息,返回的信息在 callBack 中. 如果想要获取到 小程序页面的布局信息,可以指定 ref='viewport'

入参说明

参数 类型 说明
node Dom Node 要滚动到的那个节点, 通过ref标记

结果回调(Callback)

接口调用结束的回调函数(调用成功、失败都会执行)。 callback执行后获得的信息格式如下: An image

getComponentRect示例

<template>
    <div class="wrapper" style='margin-top:200px'>
        <div ref="box" class="box">
            <text class="info">Width: {{size.width}}</text>
            <text class="info">Height: {{size.height}}</text>
            <text class="info">Top: {{size.top}}</text>
            <text class="info">Bottom: {{size.bottom}}</text>
            <text class="info">Left: {{size.left}}</text>
            <text class="info">Right: {{size.right}}</text>
        </div>
        <text class="info btn" @click='click()'>{{this.tip}}</text>
    </div>
</template>
<script>
import umsApi from 'ums-api'
function round(size) {
    var roundSize = {
        'width': Math.round(size.width),
        'height': Math.round(size.height),
        'top': Math.round(size.top),
        'bottom': Math.round(size.bottom),
        'left': Math.round(size.left),
        'right': Math.round(size.right)
    }
    return roundSize
}
export default {
    data () {
        return {
            size: {
                width: 0,
                height: 0,
                top: 0,
                bottom: 0,
                left: 0,
                right: 0
            },
            ref:"viewport",
            tip:"get box rect"
        }
    },
    mounted () {
        const result = umsApi.dom.getComponentRect(this.ref, option => {
            console.log('getComponentRect:', option)
            this.size = round.call(this,option.size);
        })
    },
    methods:{
        click:function() {
            if (this.ref === 'viewport') {
                this.ref = this.$refs.box;
                this.tip = "get viewport rect"
            } else {
                this.ref = 'viewport'
                this.tip = "get box rect"
            }
            const result = umsApi.dom.getComponentRect(this.ref, option => {
                console.log('getComponentRect:', option)
                this.size = round.call(this,option.size);
            })
        }
    }
}
</script>
<style scoped>
.btn {
    margin-top:20px;
    border-width:2px;
    border-style: solid;
    border-radius:10px;
    width:300px;
    margin-left:170px;
    padding-left:35px;
    border-color: rgb(162, 217, 192);
}
.btn:active {
    background-color: #8fbc8f;
    22border-color: gray;
}
.box {
    align-items:center;
    margin-left: 150px;
    width: 350px;
    height: 400px;
    background-color: #DDD;
    border-width: 2px;
    border-style: solid;
    border-color: rgb(162, 217, 192);
    background-color: rgba(162, 217, 192, 0.2);
}
.info {
    font-size: 40px;
    top:30px;
    margin-left:20px;
    font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
    color: #41B883;
}
</style>

addRule

addRule(rule, options) addRule是可以为 dom 添加一条规则,目前支持自定义字体fontFace规则,构建自定义的font-family,可以在text使用

rule参数说明

参数 类型 说明
rule String 为dom添加一条规则,目前只支持**'fontFace'**

options参数说明

规则参数设置,格式为 Json Object

参数 类型 说明
fontFamily String 字体名
src String 字体库位置

addRule示例

import umsApi from 'ums-api'

module.exports = {
    beforeCreate: function() {
        //目前支持ttf、woff文件,不支持svg、eot类型,moreItem at http://www.iconfont.cn/
        umsApi.domModule.addRule('fontFace', {
        'fontFamily': "iconfont2",
        'src': "url('http://at.alicdn.com/t/font_1469606063_76593.ttf')"
        });
        umsApi.domModule.addRule('fontFace', {
        'fontFamily': "iconfont3",
        'src': "url('http://at.alicdn.com/t/font_1469606522_9417143.woff')"
        });
    }
}