html不随滚动条滚动(如何div滚动时主页面不跟着滚动)

本文目录
- 如何div滚动时主页面不跟着滚动
- html某个块(广告),位于浏览器指定位置,不随滚动条滚动;
- 如何让背景图片固定不随滚动条滚动
- 网页设计如何设置网页部分内容不随着滚动条而移动移动
- 如何让DIV固定在页面而不随着滚动条随意滚动
如何div滚动时主页面不跟着滚动
在弹出DIV的时候暂时取消主页面的滚动条,关闭DIV的时候再恢复滚动条:
《html》
《head》
《style》
html,body {height:3000px}
#win {display:none; position:absolute; left:100px; top:100px; width:500px; height:300px; overflow-y:scroll; border:1px solid #000}
#content {height:2000px;}
《/style》
《script》
window.onload=function(){
var html=document.querySelector("html");
var body=document.querySelector("body");
openWin.onclick=function(){
html.style.overflow="hidden";
body.style.overflow="hidden";
win.style.display="block";
return false;
}
closeWin.onclick=function(){
html.style.overflow="visible";
body.style.overflow="visible";
win.style.display="none";
return false;
}
}
《/script》
《/head》
《body》
《a id="openWin" herf="#"》打开DIV《/a》
《div id="win"》
《div id="content"》
《a id="closeWin" herf="#"》关闭DIV《/a》
《/div》
《/div》
《/body》
《/html》
html某个块(广告),位于浏览器指定位置,不随滚动条滚动;
《!DOCTYPE html》
《html》
《head》
《title》html某个块(广告),位于浏览器指定位置,不随滚动条滚动《/title》
《meta charset="UTF-8" /》
《style》
body { height: 2000px; }
#test { position: fixed; top: 30%; right: 0%; width: 200px; height: 300px; background-color: skyblue; }
《/style》
《/head》
《body》
《div id="test"》
测试区域
《/div》
《/body》
《/html》
如何让背景图片固定不随滚动条滚动
1、打开html开发软件,新建一个html代码页面。
2、在html代码页面的《body》标签里面输入多行段落文字,用于后面浏览器滚动条滚动的时候查看图片时候随滚动条而滚动条。如图
3、设置背景图片样式。在《title》标签后面创建一个《style》标签,然后在这个标签里设置《body》标签的背景图片,以及背景图片不重复。
样式代码:
body{
background-image: url(img/bg.jpg);
background-repeat:no-repeat;
}
4、在body样式中添加上background-attachment: fixed。
5、保存html代码页面后刷新浏览器,这个时候滚动浏览器滚动条发现背景图片已经固定不随浏览器滚动而滚动了。如图
网页设计如何设置网页部分内容不随着滚动条而移动移动
div的样式中使用相当于浏览器窗口定位——position:fixed。
一、position:fixed属性的含义
fixed:生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。
我们平时所说的固定定位指的就是fixed,设置了固定定位的元素不会随滚动条上下滚动。
二、一般的 position:fixed; 实现方法
#top{position:fixed;bottom:0;right:20px}
实现了id为top的元素固定在浏览器的底部和距离右边20个像素的位置
#top{position:fixed;top:20px;right:20px}
实现了id为top的元素固定在距离浏览器的顶部20个像素和距离右边20个像素的位置
三、IE6下position:fixed; 实现方法
在IE6中是不能直接使用 position:fixed; 。你需要一些 CSS Hack 来解决它
相同的还是让 《div id="top"》...《/div》 元素固定在浏览器的底部和距离右边的20个像素,这次的代码是:#top{
position:fixed;
bottom:0;
right:20px;
_position:absolute;
_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));
}
right 跟 left 属性可以用绝对定位的办法解决,而 top 跟 bottom 就需要用上面的表达式来实现。其中在_position:absolute; 中的 _ 符号只有 IE6 才能识别,目的是为了区分其他浏览器
1、使元素固定在浏览器窗口的顶部:
#top{
_position:absolute;
_top:expression(eval(document.documentElement.scrollTop));
}
2、使元素固定距浏览器窗口的顶部a像素的位置:
#top{
_position:absolute;
_top:expression(eval(document.documentElement.scrollTop));
_margin-top:a;
}或者
#top{
_position:absolute;
_top:expression(eval(document.documentElement.scrollTop+a));
}
3、使元素固定在浏览器窗口的底部:
#top{
_position:absolute;
_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));
}
4、使元素固定在距浏览器窗口的底部b像素的位置:
#top{
_position:absolute;
_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));
_margin-bottom:b;
}或者
#top{
_position:absolute;
_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||b)-(parseInt(this.currentStyle.marginBottom,10)||b)));
}
四、IE6下的闪动问题
问题还没有完全解决。在用了上面的办法后,你会发现:被固定定位的元素在滚动滚动条的时候会闪动。解决闪动问题的办法是在 CSS 文件中加入:
*html{background-image:url(about:blank);background-attachment:fixed;}
其中 * html选择器hack是给 IE6 识别的。
到此,IE6 的 position:fixed; 问题已经被解决了
如何让DIV固定在页面而不随着滚动条随意滚动
你可以对div设置css属性,相对于页面定位:
position:fixed;
position属性再搭配top、bottom、right、left确定div位于页面的具体位置,比如:
top:10px; right:20px;
另外,因为div是相对与页面定位,在滚动条滚动时,其他的html元素可能因为在它之上而将它遮挡上(如果你的这个div先写的,后又写很多div什么的,就可能将它压载下面看不到了),如果希望这个div一直保持可见(也就是在最上层),可以再设置属性z-index:
z-index:999; //数值大就在上面
希望对你有帮助。

更多文章:
directive vue(如何在Vue中建立全局引用或者全局命令)
2026年5月6日 19:15
javaeclipse包的新建(用eclipse如何创建java工程)
2026年1月11日 08:30
c语言switch语句case后面接什么(c语言switch语句中case后面必须要接整型常量和字符型常量吗)
2025年10月26日 08:15
怎么把普通文件导入变成web项目(java项目怎么转成web项目)
2025年9月18日 16:00
在循环体内使用break语句(在循环结构中使用break语句,退出的是一层循环,还是多重循环呢可以给出一个简单且巧妙的例子吗)
2026年5月1日 00:00
position在css的属性(CSS中position属性详解)
2025年9月26日 23:45
amazed是什么牌子(amazing和amazed有什么区别)
2026年4月3日 15:00
周期函数值域的求法(已知函数的最小正周期为求;当时,求函数的值域.)
2025年11月30日 22:15
毕设做网站必须要用到ssm框架嘛(毕业设计做ssm还是app更难)
2026年3月31日 03:00
new directory(如何将威纶通tk6070ip触摸屏程序上传到电脑里)
2026年8月7日 01:45















