我希望图像在向下滚动页面时向上滚动。我可以很容易地做到这一点,但我遇到的问题是,在jQuery中,我正在更改图像的CSS,特别是其transform
属性。但我已经使用transform
属性来居中对齐图像。更改滚动div的位置
代码:
.planeImg {
position: absolute;
display: block;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: url("../images/plane.png");
background-size: contain;
background-repeat: no-repeat;
z-index: 99;
height: 80px;
width: 300px;
}
$(window).on("scroll", function() {
var wScroll = $(this).scrollTop();
$('.planeImg').css({'transform' : 'translate(0px, -'+ wScroll /2 +'%)'})
});
,是因为我使用的变换属性中心对齐形象,jQuery是buggering了我的图像的位置。
你设定的水平转换成'0px'在你的JavaScript。你可以用'-50%'代替它来保持水平居中。垂直也是一个问题吗? –
你应该使用top/left/bottom/right(或边距等)来定位图像而不是transform属性。 – vicgoyso
Thanks that worked @JonUleis – RhysE96