[英]CSS Parallax Background
I am currently developing https://dev.shivampaw.com/me and I have a class called "parallax-bg" which should make a parallax background. 我目前正在开发https://dev.shivampaw.com/me ,我有一个名为“ parallax-bg”的类,该类应具有视差背景。
For my contact (section) right at the bottom, it isn't working yet for my hero (top) and texture (middle) it works fine. 对于我底部的联系人(部分)而言,它对我的英雄(顶部)和纹理(中部)而言仍然无法正常工作。
This is the jQuery I am using: 这是我正在使用的jQuery:
$(window).scroll(function() {
var scrolledY = $(window).scrollTop();
$('.parallax-bg').css('background-position', 'center ' + ((scrolledY) - 70) + 'px');
});
And the CSS for the contact form section is just: 联系人表单部分的CSS只是:
section#contact{
background: linear-gradient(to bottom, rgba(0,0,0, 0.65) 0%,rgba(0,0,0, 0.65) 100%), url(https://www.shivampaw.com/images/bg-contact.jpg) no-repeat center -70px;
background-size: cover;
background-attachment: scroll;
color: white;
}
And for the hero section: 对于英雄部分:
section#hero{
background: linear-gradient(to bottom, rgba(0,0,0, 0.65) 0%,rgba(0,0,0, 0.65) 100%), url(https://www.shivampaw.com/images/top-bg.jpg) no-repeat center -70px;
background-size: cover;
background-attachment: scroll;
text-align: center;
color: white;
font-weight: bold;
padding-bottom: 50px;
}
Does anyone know why this is happening? 有人知道为什么会这样吗?
Thanks 谢谢
Update: by parallax I mean fixed background. 更新:视差指的是固定背景。 I use jquery so it works on mobiles as well.
我使用jquery,因此它也可以在手机上使用。
I fixed this by changing my jQuery to the following: 我通过将jQuery更改为以下内容来解决此问题:
$(window).on('scroll resize load',function(){
// 500px
var scrolledY = $(window).scrollTop();
$('.parallax-bg').each(function(){
$obj = $(this);
var pixelsY = $obj.data("position");
$obj.css('background-position', 'center ' + ((scrolledY) + pixelsY) + 'px');
});
});
And add a data attribute called position in my html where the class parallax-bg is. 并在我的html类parallax-bg所在的位置添加一个名为position的数据属性。 This represented my desired background position y offset which fixed the problem.
这代表了我想要的背景位置y偏移量,从而解决了该问题。
I also removed the no-repeat from the CSS. 我还从CSS中删除了不可重复的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.