search
HomeWeb Front-endHTML TutorialFix the position of div through css/js_html/css_WEB-ITnose

1: Let’s take a look at using css first. You need to write expressions in css, but it flashes and flashes. Assign the variable "ignoreMe", and "document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop" (this is a bug in IE).

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <HEAD> 4 <TITLE> css test </TITLE> 5 <style> 6 #LoadingStatus{ 7 position:fixed !important; 8 position:absolute; 9 top:100px;10 top: expression( ( 100 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );11 margin:0 0 0 -110px;12 width:220px;height:19px;13 left:50%;14 text-align:center;15 border:1px solid red;16 }17 </style>18 </HEAD>19 <BODY>20   <div id="LoadingStatus" style="display:none;"> loading...</div>21     <div style="height:1024px">22         <input type="button" onclick="document.getElementById('LoadingStatus').style.display=''" value='显示loading' />23       </div> 24       <div>25       <input type="button" onclick="document.getElementById('LoadingStatus').style.display=''" value='显示loading' />26     </div>27 </BODY>28 </HTML>

2: Written in JS, it can actually be applied to css. It is no different from the above. It should be noted that under FF, the upper left and upper right are still floating. Under IE, everything is fixed, but flickering. Which should be used with caution.

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <HTML> 3 <HEAD> 4 <TITLE>JAVASCRIPT</TITLE> 5 <META HTTP-EQUIV="Content-Type" CONTENT="text/html" charset="UTF-8"> 6 </HEAD> 7 <style> 8 <!-- 9 .div {10     position: absolute;11     border: 2px solid red;12     background-color: #EFEFEF;13     line-height:90px;14     font-size:12px;15     z-index:1000;16 }17 -->18 </style>19 <BODY>20 <div id="Javascript.Div1" class="div" style="width: 240px; height:90px" align="center">正中...</div>21 <SCRIPT LANGUAGE="JavaScript">22 function sc1(){23 document.getElementById("Javascript.Div1").style.top=24    document.documentElement.scrollTop+(document.documentElement.clientHeight-document.getElementById("Javascript.Div1").offsetHeight)/2+"px"25 document.getElementById("Javascript.Div1").style.left=26    document.documentElement.scrollLeft+(document.documentElement.clientWidth-document.getElementById("Javascript.Div1").offsetWidth)/2+"px";27 }28 </SCRIPT>29 <div id="Javascript.Div2" class="div" style="width: 240px; height:90px;" align="center">左上...</div>30 <SCRIPT LANGUAGE="JavaScript">31 function sc2(){32 document.getElementById("Javascript.Div2").style.top=document.documentElement.scrollTop;33 document.getElementById("Javascript.Div2").style.left=document.documentElement.scrollLeft;34 }35 </SCRIPT>36 <div id="Javascript.Div3" class="div" style="width: 240px; height:90px;" align="center">左下...</div>37 <SCRIPT LANGUAGE="JavaScript">38 function sc3(){39 document.getElementById("Javascript.Div3").style.top=40     document.documentElement.scrollTop+document.documentElement.clientHeight-document.getElementById("Javascript.Div3").offsetHeight+"px";41 document.getElementById("Javascript.Div3").style.left=document.documentElement.scrollLeft;42 }43 </SCRIPT>44 <div id="Javascript.Div4" class="div" style="width: 240px; height:90px;" align="center">右上...</div>45 <SCRIPT LANGUAGE="JavaScript">46 function sc4(){47 document.getElementById("Javascript.Div4").style.top=document.documentElement.scrollTop;48 document.getElementById("Javascript.Div4").style.left=49     document.documentElement.scrollLeft+document.documentElement.clientWidth-document.getElementById("Javascript.Div4").offsetWidth+"px";50 }51 </SCRIPT>52 <div id="Javascript.Div5" class="div" style="width: 240px; height:90px;" align="center">右下...</div>53 <SCRIPT LANGUAGE="JavaScript">54 function sc5(){55 56 document.getElementById("Javascript.Div5").style.top=57     document.documentElement.scrollTop+document.documentElement.clientHeight-document.getElementById("Javascript.Div5").offsetHeight+"px";58 document.getElementById("Javascript.Div5").style.left=59     document.documentElement.scrollLeft+document.documentElement.clientWidth-document.getElementById("Javascript.Div5").offsetWidth+"px";60 }61 </SCRIPT>62 <SCRIPT LANGUAGE="JavaScript">63 <!--64 function scall(){65 sc1();sc2();sc3();sc4();sc5();66 }67 window.onscroll=scall;68 window.onresize=scall;69 window.onload=scall;70 //-->71 </SCRIPT>72 <div style="position: absolute; top: 0; left: 0; width: 10000px; height: 4000px;"></div>73 </BODY>74 </HTML>

3: This is still written in js, but it will not flash during the scrolling process of the IE browser. The compatibility effect is very good.

 1 <!DOCTYPE HTML> 2   <html> 3   <head> 4     <meta http-equiv="content-type" charset="utf-8" /> 5     <title>Js跟随滚动条移动的DIV</title> 6   </head> 7   <style type="text/css"> 8   *{margin:0;padding: 0;background-color:#000; } 9   div#con{width: 800px;margin: 0 auto;}10   div.pop{width:800px;font-size: 18px;background:yellow;padding:20px 0;margin:20px auto;border:1px solid yellow;text-align: center;color: #000;}11   p{width:800px;padding:20px 0;background:#fff;border:1px solid #000;margin:20px auto;text-align: center;}12   div.active{position: fixed;top: 100px;z-index: 100;margin: 0;_position: absolute;}13   </style>14   <body>15   <div id="con" style="height:2000px;">16   <p>我是元素p</p>17   <div id="myDiv" class="pop active">18     我是随屏幕滚动的DIV,我距离顶部高度适中为100px,修改css就可以修改该值19   </div>20   21   <p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p>我是元素p</p><p style="color:red;position:fixed;top:400px;">我是元素top:400px;看是否支持固定定位</p>22   </div>23     <script type="text/javascript">24     (function(){25         //if(navigator.userAgent.toLocaleLowerCase().indexOf('msie 6.0;')>1){26         if(checkFixed()){27             return ;28         }//只需对不支持固定定位的浏览器做处理29         30         //将下面需要使用的dom相关操作在scrollDiv外面提取一次,避免多次dom操作31         var fixedObj = document.getElementById("myDiv"),32             height = fixedObj.offsetHeight,//标签高度提前读取出来33             firstTop =getTop(fixedObj),//记录页面一开始标签所在位置34             timer;35             window.onscroll = function(){//这里做个延时执行,可以减少scroll执行频率36                 clearTimeout(timer);37                 timer = setTimeout(function(){38                     scrollDiv()39                 },30);40             }41 42         function scrollDiv(){43             var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;44             fixedObj.style.top=(firstTop+scrollTop)+'px';45         }46         //获取元素在页面里top值47         function getTop(obj) {48             var top = 0;49             while(obj){50                 top += obj.offsetTop;51                 obj = obj.offsetParent;52             }53             return top;54         }55         //判断浏览器是否支持固定定位56         function checkFixed(){57             var o = document.createElement('div'),58                 body = document.getElementsByTagName('body')[0],59                 fig = false;60             body.insertBefore( o, body.firstChild );61             o.style.cssText ='position:fixed;top:20px;margin:0;padding:0;border:none;width:0px;height:0px;';62             fig = o.offsetTop==20;63             body.removeChild(o);64             return fig;65         }66     })()67   </script>68   </body>69   </html>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)