常见兼容性问题:吸顶元素无法固定,会随着页面滚动抖动
解决方案:页面整体为弹性布局,中间加载部分自适应高度,总体结构为 顶部固定+内容+底部固定
注意整体弹性布局的时候如果要实现吸顶效果,必须将定位元素放到flex=1元素的外层,吸顶的元素需要用两个div,一个是正常显示的,一个是滚动到一定高度固定到顶部的
html:
<div class="wrap" id="wrapId">
<div class="isFixed" v-if="is_fixed">
<div class="topBar" id="fixedTopFixed" ref="topBar">
<div class="item" v-for="(item,index) in barList" >吸顶内容</div>
</div>
</div>
<div class="flexWrap" :class="is_fixed? 'wrapTop' : 'flex'">
<div class="myScroll" v-infinite-scroll="loadMore" infinite-scroll-throttle-delay="500" infinite-scroll-immediate-check="true" infinite-scroll-disabled="busy" infinite-scroll-distance="50">
<div class="flexContent">
<div class="top" ref="top">
<div class="banner"><img src="../../../assets/images/文件名/banner.jpg" alt="" srcset=""></div>
<div class="topBar" id="fixedTop" ref="topBar" v-show="!is_fixed">
<div class="item" v-for="(item,index) in barList" @click="tab(index,item)">
不吸顶时展示的内容
</div>
</div>
</div>
<div class="scrollContent" id="wrap-content" ref="contentH">
<div class="memberList" v-show="infoList.length>0" id="content">
<div class="myScroll" v-infinite-scroll="loadMore" infinite-scroll-throttle-delay="500" infinite-scroll-immediate-check="true" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
<div class="memberItem" v-for="(item,index) in infoList">
加载内容
</div>
<div class="loading" v-if="loading">
<span id="load-text">{{loadText}}</span>
</div>
</div>
</div>
<div class="empty" v-show="noData">最新达成情况正在更新中...请稍后再来~</div>
</div>
</div>
</div>
</div>
<div class="footer">底部固定</div>
</div>
js:
data: {
return {
busy: false,
pageNum: 1,
pageSize: 10,
loading: false,
noData: false,
infoList: []
}
}
mounted() {
<!--监听滚动-->
that.$nextTick(() =>{
let scrollDOM = document.querySelector('.flexContent')
scrollDOM.addEventListener('scroll',that.handleScroll)
})
},
methods: {
handleScroll () {
let scrollDOM = document.querySelector('.flexContent')
let scrollTop = scrollDOM.scrollTop
<!--计算滚动高度-->
let clientHeight = document.documentElement.clientHeight
if (scrollTop >document.querySelector('.banner').offsetHeight) {
this.is_fixed = true
} else {
this.is_fixed = false
}
},
<!--//触发加载-->
loadMore() {
if(this.pageNum<this.pages) {
this.loading = true
this.pageNum+=1
this.busy = true
this.loadData(接口参数,this.pageNum)
}
},
//加载时触发的接口调用
loadData() {
api.XXX({},function(success,data,err){
if(success) {
if(data.status==200) {
//加载逻辑判断
if(data.body.list.length>0 &&pageNum>0){
that.infoList = that.infoList.concat(data.body.list)
that.loading = false
}
if(pageNum==data.body.pages || data.body.list<20) {
that.loading = true
that.loadText = '没有更多数据了'
}
if(pageNum==1 &&data.body.list.length==0) {
that.noData = true
that.infoList = []
}
}
}
})
}
}
css:
.isFixed {
width: 100%
height: 1rem
position: absolute
top: 0
left: 0
z-index: 100
}
.myScroll {
height: 100%
}
.wrap {
width: 7.5rem
margin: auto
height: 100%
overflow: hidden
display: flex
flex-direction: column
}
.flex {
flex: 1
}
.wrapTop {
padding-top:0.45rem
}
.flexWrap {
width: 100%
height: 100%
}
.flexContent {
width: 100%
height: 100%
display: flex
flex-direction: column
overflow: scroll
-webkit-overflow-scrolling: touch
}
.top {
.banner {
position: relative
img {
width: 100%
display: block
}
.month {
position: absolute
bottom: 0.33rem
left: 0.45rem
color: #fff
font-size: 0.45rem
}
}
}
.scrollContent {
width: 100%
background: #fff
flex: 1
-webkit-overflow-scrolling: touch
.memberList {
height: 100%
padding-top: 0.2rem
}
}
.footer {
position: fixed
bottom: 0
width:7.5rem
margin: auto
height:auto
background:rgba(0,0,0,.7)
}
很简单,在js里,float属性不能直接用style.float=""来设定,在ie下面用style.styleFloat=""在moz浏览器里用style.cssFloat="" 来设定。故楼主代码应改成如下:
<html>
<head>
<title>左右布局</title>
</head>
<body>
<script type="text/javascript">
var mainDiv = document.createElement("div")
mainDiv.style.position = "absolute"
mainDiv.style.width = "700px"
mainDiv.style.left = "100px"
mainDiv.style.tops = "100px"
mainDiv.style.height = "384px"
mainDiv.style.background = "red"
var leftDiv = document.createElement("div")
leftDiv .style.height = "384px"
leftDiv .style.width = "50%"
leftDiv .style.styleFloat = "left"//for ie
leftDiv .style.cssFloat = "left"//for moz
leftDiv .style.clear = "none"
leftDiv .style.background = "yellow"
var rightDiv = document.createElement("div")
rightDiv .style.height = "384px"
rightDiv .style.width = "50%"
rightDiv .style.styleFloat = "right"//for ie
rightDiv .style.cssFloat = "right"//for moz
rightDiv .style.clear = "none"
rightDiv .style.background = "green"
mainDiv.appendChild(leftDiv)
mainDiv.appendChild(rightDiv)
prompt("",mainDiv.outerHTML)
document.body.appendChild(mainDiv)
</script>
</body>
</html>
你用prompt导出的是没有经过浏览器解析的js直接转换成css的代码,当然可以达到效果。。。
首先你要明白你想要的效果,然后找到相似的js代码,一般来说都可能需要修改才能得到你自己真正想要的效果,如果要修改,那你就必须要懂js才可以修改了,这个是必须的。所以还是建议你去学习js,js可以控制整个DOM,例如设置元素的CSS样式。也可以设置动作。例如:onclick事件。也可以不刷新页面获取数据:ajax技术。等等这些都可以用js实现。所以懂一点js还是必须的。