美文网首页
小程序获取页面高度,设置背景色

小程序获取页面高度,设置背景色

作者: 後弦月的雨 | 来源:发表于2019-05-15 10:19 被阅读0次

一、单独页面分开调用

index.wxml

<scroll-view scroll-y style='height:{{scroll_height}}rpx;background-color: #f2f2f2;'> </scroll-view>

index.js

Page({

    data: {

        scroll_height: 0,

    },

    onLoad: function() {

        let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度

        let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度

        this.setData({

            scroll_height: windowHeight * 750 / windowWidth

        })

    },

})

二、在小程序中写成公用方法

index.wxml

<scroll-view scroll-y style='height:{{scroll_height}}rpx;background-color: #f2f2f2;'> </scroll-view>

index.js

Page({

    data: {

        scroll_height: 0,

    },

    onLoad: function() {  // 获取页面高度,设置背景色

        var that = this;

        app.scrollHeight(that);

    },

})

app.js

scrollHeight: function(that) { // 获取页面高度,设置背景色,公用方法

        let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度

        let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度

        that.setData({

            scroll_height: windowHeight * 750 / windowWidth

        })

    },

隐藏滚动条

::-webkit-scrollbar {

    width: 0;

    height: 0;

    color: transparent;

}

相关文章

网友评论

      本文标题:小程序获取页面高度,设置背景色

      本文链接:https://www.haomeiwen.com/subject/bibuaqtx.html