javascript - Changing Image position when scrolling page
问题描述
I'm new to javascript and wanna create a HTML page and would like to make an image like floating on my page and it will interacting when user scrolling up and down of the website but I just couldn't figure it out. Anyone has idea how to deal with it?
<script>
$(window).scroll(function () {
//You've scrolled this much:
// $('p').text("You've scrolled " + $(window).scrollTop() + " pixels");
var doc = doc
var x = document.getElementsByClassName("ImageTesting");
//x.style.top = "$(window).scrollTop()";
x.css({'top': $(window).scrollTop() +'px'});
console.log($(window).scrollTop() + "px")
//console.log(x.style.top)
});
</script>
解决方案
您可以使用上面首先提到的简单 CSS 属性来做到这一点 带有图像标签的 CSS Sticky 属性或只是
<div class="bgFloat"></div>
<style>
.bgFloat {
background-image: url(image.jpg);
background-attachment: fixed;
background-position: center;
background-size: contain;
background-repeat: no-repeat;
}
</style>
它将帮助您将图像位置固定为背景。或者,如果您想继续使用图像标签,请按照 'position:sticky; top:0' 或您想要放置元素的任何位置。
推荐阅读
- jquery - 当href有标签时如何解决jquery异常
- php - Preg 根据字符串替换文本
- json - pandas 字典行到列
- swift - 调度组不通知?
- python - 在 Python3 中使用优先队列进行堆栈
- python - 具有多个根元素的“XML”文档
- python - 将 .txt 文件中的元素作为列表而不是字符串打开
- python - 如何使用 Django 中的选择 - 如何显示特定内容?
- android - 该位置没有 XML:http://schemas.android.com/apk/res/android
- ruby-on-rails - 如何从静态站点向 Rails 应用程序发布请求?