CSS3 移动端 滚动置顶 吸顶

html-css09

CSS3 移动端 滚动置顶 吸顶,第1张

如果是在回调里置顶DIV的话(比如点击事件),可以用scrollIntoView。 scrollIntoView如果不想吸顶 可以加个before伪类设置高度 CSS3滚动置顶目前有两种解决方案: 第一种(主流):transform: translate3d(x,y,z) 第二种(未来):position: stickytop:xxx 假设需求: 需求一:滚动过程中A ,C区域不吸顶,B区域吸顶 VUE框架推荐直接使用vantUI插件,indexbar索引栏。https://youzan.github.io/vant/#/zh-CN/index-barvantUI原理用的是transform。 以上面图片为例大致讲解: 1.transform对JS的能力要求高些,页面加载完毕后,给每个B区域对应的DIV设置translate3d的Y轴值,值为B对应div离顶部的高度。 2.当开始滚动起来后,Y轴值为原来值减去滚动条滚动距离值 3.当Y轴值小于0的时候,锁死为0,就自动吸顶了 4.当待置顶DIV的translate3d Y轴值小于DIV的高度时,已置顶DIV的Y轴值开始变为负数,这样会有一个顶出的效果 5.已置顶DIV被待置顶DIV顶出一定距离(一般两倍DIV高度),取消translate3d属性, 6.回滚反向计算即可 需求二:滚动过程中 C不吸顶,B吸顶,A一直吸顶 B在A下面 这个需求vantUI(v2.9.3)目前解决不了,采用translate太复杂,决定采用CSS3新属性position: sticky。 如果是2018年使用该属性兼容性还是个问题,但来到2020年下半年,对大部分生产环境都不是问题。博主测试的IOS11和安卓小米 华为 都支持该属性。博主的建议是:如果是公司APP内嵌H5页面或者微信打开可以使用该属性,如果纯H5页面 ,建议translate. position: sticky字面意思就是粘性定位。 可以粘顶部也可以粘底部 右侧 左侧。 所以除了position: sticky还要给一个定位值,比如top:0px或者right:0px就可以了 非常简单。 比如直接给Bdiv设置下面样式就行 实际使用要注意下面几点: 1.如果想兼容IOS12 IOS11 必须带position: -webkit-sticky而且要在样式表里写,不能写在style里。 2.容器相关。只有要移除容器范畴才起作用。比如body的height不要设置100%,height:100%表示所有元素一直在屏幕范围

这样写吧:

<div class="meter">

<span style="width: 25%"></span>

</div>

.meter {

height: 20px /* Can be anything */

position: relative

background: #555

-moz-border-radius: 25px

-webkit-border-radius: 25px

border-radius: 25px

padding: 10px

box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3)

}

.meter >span {

display: block

height: 100%

border-top-right-radius: 8px

border-bottom-right-radius: 8px

border-top-left-radius: 20px

border-bottom-left-radius: 20px

background-color: rgb(43,194,83)

background-image: linear-gradient(

center bottom,

rgb(43,194,83) 37%,

rgb(84,240,84) 69%

)

box-shadow:

inset 0 2px 9px rgba(255,255,255,0.3),

inset 0 -2px 6px rgba(0,0,0,0.4)

position: relative

overflow: hidden

}

.orange >span {

background-color: #f1a165

background-image: linear-gradient(to bottom, #f1a165, #f36d0a)

}

.red >span {

background-color: #f0a3a3

background-image: linear-gradient(to bottom, #f0a3a3, #f42323)

}

.meter >span:after {

content: ""

position: absolute

top: 0left: 0bottom: 0right: 0

background-image: linear-gradient(

-45deg,

rgba(255, 255, 255, .2) 25%,

transparent 25%,

transparent 50%,

rgba(255, 255, 255, .2) 50%,

rgba(255, 255, 255, .2) 75%,

transparent 75%,

transparent

)

z-index: 1

background-size: 50px 50px

animation: move 2s linear infinite

border-top-right-radius: 8px

border-bottom-right-radius: 8px

border-top-left-radius: 20px

border-bottom-left-radius: 20px

overflow: hidden

}

一、实现CSS3无限循环动画代码示例。

代码如下:

CSS:

@-webkit-keyframes gogogo {

0%{

-webkit-transform: rotate(0deg)

border:5px solid red

}

50%{

-webkit-transform: rotate(180deg)

background:black

border:5px solid yellow

}

100%{

-webkit-transform: rotate(360deg)

background:white

border:5px solid red

}

}

.loading{

border:5px solid black

border-radius:40px

width: 28px

height: 188px

-webkit-animation:gogogo 2s infinite linear

margin:100px

}

扩展资料

实现动画无限循环所需要的CSS属性说明:

1、infinite

在animation后面加上infinite就可以无限循环,另外还可以做反向循环使用animation-direction

2、animation-name

规定需要绑定到选择器的 keyframe 名称。

3、animation-duration

规定完成动画所花费的时间,以秒或毫秒计。

4、animation-timing-function

规定动画的速度曲线。

5、animation-delay

规定在动画开始之前的延迟。

6、animation-iteration-count

规定动画应该播放的次数。

7、animation-direction

规定是否应该轮流反向播放动画。