如何将元素放置在距浏览器顶部固定距离的位置?

use*_*804 2 css

我想定位一个元素,使其始终与浏览器顶部保持一定的距离,这样即使用户向下滚动,图像也始终与屏幕顶部的距离相同.我该怎么做呢?

Ble*_*der 8

这里不需要jQuery.纯CSS就足够了:

#element {
   position: fixed;
   top: 100px;
}
Run Code Online (Sandbox Code Playgroud)

但是如果你坚持使用jQuery解决方案,下面的代码通过jQuery设置CSS:

$('#element').css({'position': 'fixed', 'top': '100px'});
Run Code Online (Sandbox Code Playgroud)