zoukankan      html  css  js  c++  java
  • uniapp吸顶功能

    <template>
    	<view class="about">		
    		<view class="lis">			
    			 <view class="li" v-for="(item,index) in 100" :key="index">
    				{{index}}
    			 </view>
    		</view>
    		<!-- :style="{top:topHeight+'upx'}" -->
    		 <view :class="[(isFixedTop) ? 'fixedTop' : '']" id="box" class="box">
    		 	吸顶导航
    		 </view>
    		 <view class="lis">
    		 	 <view class="li" v-for="(item,index) in 50" :key="index">
    		 		{{index}}
    		 	 </view>
    		 </view>
    	</view>
    </template>
    
    <script>
    	export default {
    		data() {
    			return {
    				isFixedTop:false,
    				Topdistance:3000,
    				topHeight:"",
    			}
    		},
    		onLoad() { 
    			uni.pageScrollTo({
    				scrollTop:0,
    				duration:0
    			})
    			setTimeout(()=>{
    				this.GetTop()
    				// this.aaa()
    			},1000)
    		},
    		onPageScroll(e) {
    			console.log(e.scrollTop)
    			if(e.scrollTop > this.Topdistance){
    				this.isFixedTop=true
    			}else{
    				this.isFixedTop=false
    			}
    		},
    		methods: { 
    			aaa(){			 
    				uni.createSelectorQuery().in(this).select('.uni-page-head').boundingClientRect(result => {
    					 if (result) { 
    						this.topHeight=result.height * 2
    						console.log(this.topHeight)
    					}else { 
    						 
    					} 
    				 }).exec(); 			 
    			},	
    			GetTop(){
    				//获取元素距离顶部的距离
    				var _this=this
    				uni.getSystemInfo({
    					success:(resu)=>{	
    						console.log(resu)
    						const query = uni.createSelectorQuery()
    						query.select('#box').boundingClientRect()
    						query.selectViewport().scrollOffset()
    						query.exec(function(res){
    							_this.Topdistance=res[0].top
    							console.log(res)
    						})
    					},
    					fail:(res)=>{}
    				})
    			}, 
    			
    		}
    		
    	}
    </script>
    
    <style>
    	 .li{
    		 height: 100upx;
    		 line-height: 100upx;
    		 margin-top: 20upx;
    		 text-align: center;
    		 color: #FFFFFF;
    	 }
    	 .lis :nth-child(3n+1){
    		 opacity: 0.1;
    		 background-color: #f00;
    	 }
    	 .lis :nth-child(3n+2){
    		  opacity: 0.1;
    	 	 background-color: #0f0;
    	 }
    	 .lis :nth-child(3n+3){
    		  opacity: 0.1;
    	 	 background-color: #00f;
    	 }
    	 .box{
    		 height: 100upx;
    		 line-height: 100upx; 
    		 text-align: center;
    		 background-color: #f0f;
    		 color: #fff;
    	 }
    	 .fixedTop{
    		 position: fixed;
    		 top:0;
    		 left: 0;
    		 right: 0;
    		 z-index: 9999999;
    	 }
    </style>
    

      

  • 相关阅读:
    实例化讲解RunLoop---IOS
    IOS中的SpringBoard
    Mac版OBS设置详解
    Swift学习Tip之Closures
    Swift中Array的Map
    IOS中的国际化(一)
    IOS,中获取系统内存占有率的方法
    IOS获取两个日期之间的时间间隔
    IOS中微信支、支付宝支付详解
    IOS中的正则表达式:NSRegularExpression
  • 原文地址:https://www.cnblogs.com/daifuchao/p/13647044.html
Copyright © 2011-2022 走看看