html固定背景视觉差,试着写一个全屏背景视觉差滚动效果的代码

试着写一个全屏背景视觉差滚动效果的代码。

HTML

page1
page2
page3

CSS

html,body{

height: 100%;

margin: 0;

}

.wrap{

width: 100%;

height: 100%;

overflow-x: hidden;

overflow-y: scroll;

position: relative;

}

.item{

display: block;

width: 100%;

height: 100%;

font-size: 50px;

text-align: center;

position: relative;

background-attachment: fixed;

}

/*分别设置item的背景*/

.one{

background: url(img/banner1.jpg) no-repeat top left / 100% 100%;

}

.two{

background: url(img/banner2.jpg) no-repeat top left / 100% 100%;

}

.three{

background: url(img/banner3.jpg) no-repeat top left / 100% 100%;

}

/*用伪类给item增加mask效果*/

.item::before{

content: '';

position: absolute;

left: 0;

top: 0;

background-color: black;

opacity: 0.3;

width: 100%;

height: 100%;

}

.content{

position: absolute;

background-color: rgba(255,255,255,0.4);  /*设置文本背景的透明度但是又会让子元素继承到*/

border-radius: 20px;

width: 300px;

height: 100px;

top: 50%;

left: 50%;

transform: translate(-150px,-50px);

}

JS

var wrap = document.getElementsByClassName('wrap')[0];

var items=document.getElementsByClassName('item');

var clientHeight=document.body.clientHeight;

var num = 0;

//窗口改变时的自适应

window.οnresize=function(){

clientHeight=document.body.clientHeight;

for (var i=0;i

items[i].style.height=clientHeight;

}

}

wrap.addEventListener("scroll", function() {

num = wrap.scrollTop;

for (var i=0;i

items[i].style.backgroundPositionY=num-i*clientHeight +'px';//根据滚动条的位置改变item的backgroundPosY

}

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值