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

一、单独页面分开调用

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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容