Div for fixing at the top of the window

Guys, I want to fix the width and height of the div as 100%. But the problem is that the div is inside a fixed-width wrapper.

I have a button above the div that onclick = "" causes the div to change its class with full width and height. I want to put this div in the upper left corner of the window. My code

<html> <head> <title>Javascript Change CSS Class of Div tag</title> <style type="text/css"> #wrapper { width:75%; height:75%; margin:0 auto; } .minimize { color : red; width:500px; height:200px; background:#474747; float:left; } .maximize { color : blue; width:100%; height:100%; float:left; background:#ccc; } </style> <script language="javascript" type="text/javascript"> function changeCssClass(navlink) { if(document.getElementById(navlink).className=='minimize') { document.getElementById(navlink).className = 'maximize'; } else { document.getElementById(navlink).className = 'minimize'; } } </script> </head> <body> <div id="wrapper"> <div id="navlink" class="minimize"><input type="button" value="click here" onclick="changeCssClass('navlink')" /> </div> </div> </body> </html> 

But I want it to look like this with a wrapper

 <html> <head> <title>Javascript Change CSS Class of Div tag</title> <style type="text/css"> .minimize { color : red; width:500px; height:200px; background:#474747; float:left; } .maximize { color : blue; width:100%; height:100%; float:left; background:#ccc; } </style> <script language="javascript" type="text/javascript"> function changeCssClass(navlink) { if(document.getElementById(navlink).className=='minimize') { document.getElementById(navlink).className = 'maximize'; } else { document.getElementById(navlink).className = 'minimize'; } } </script> </head> <body> <div id="navlink" class="minimize"><input type="button" value="click here" onclick="changeCssClass('navlink')" /> </div> </body> </html> 

Did anyone here help ...

If anyone has any suggestions?

+4
source share
7 answers

I think this is what you need, but it was hard to say because you did not specify exactly which states should be saved for .minimize and .maximize.

Please note that javascript is significantly different from your original.

Since the "class" is an attribute of the DOM elements, it should be accessed using getAttribute and setAttribute. IE6 had a very, very old bug that allowed javascript to access class classes via className, but that is no longer the case.

Also, pay attention to how I handle the class attribute. Since you can specify multiple classes for an element, this code takes this into account. You can safely add additional classes without fijing with maximization and minimization.

The second thing to watch is css. Use position: fixed locks the item in position regardless of the scroll value. In this example, there are two ways to set the div in full screen mode. The first indicates 100% width and height. However, it is fragile.

It is better to set the top, right, bottom, and left values ​​to 0. This gives you more control. Also, suppose you need thin edges around the edges. Instead of worrying about mixing the top and left with the width and height, you can simply specify a pixel or percentage value for the 4 properties that I mentioned to get a simple, uniform look.

+4
source

I checked the Berker fiddle and it will fix your problem. Sowmya uses this fiddle, but I made a few changes, check this out:

Since class is an attribute of DOM elements, it should be accessed using getAttribute and setAttribute . IE6 had a very, very old bug that allowed javascript to access element classes through className , but that is no longer the case.

+3
source

Take a look at this script, http://jsfiddle.net/Tv2pP/7/

I think this is what you need, but it was hard to say because you did not specify exactly which states should be saved for .minimize and .maximize.

Please note that javascript is significantly different from your original.

Since the "class" is an attribute of the DOM elements, it should be accessed using getAttribute and setAttribute. IE6 had a very, very old bug that allowed javascript to access class classes via className, but that is no longer the case.

Also, pay attention to how I handle the class attribute. Since you can specify multiple classes for an element, this code takes this into account. You can safely add additional classes without fijing with maximization and minimization.

The second thing to watch is css. Use position: fixed locks the item in position regardless of the scroll value. In this example, there are two ways to set the div in full screen mode. The first indicates 100% width and height. However, it is fragile.

It is better to set the top, right, bottom, and left values ​​to 0. This gives you more control. Also, suppose you need thin edges around the edges. Instead of worrying about mixing the top and left with the width and height, you can simply specify a pixel or percentage value for the 4 properties that I mentioned to get a simple, uniform look.

Finally, if you have the option, you should use a standardized library such as jQuery. This has become an incredibly useful tool over the years in order to do just such things without you, the developer, too worried about discrepancies in the underlying browser platform.

+1
source

Remove margin:0 auto from shell class

Check out http://jsfiddle.net/PAj39/

+1
source

Take a look at this script, http://jsfiddle.net/Tv2pP/7/

I think this is what you need, but it was hard to say because you did not specify exactly which states should be saved for .minimize and .maximize.

+1
source

Remove margin:0 auto from shell class

Check out http://jsfiddle.net/PAj39/


In the method below, the inner div aligns the top left to the browser

add position:fixed; top:0; left:0 position:fixed; top:0; left:0 position:fixed; top:0; left:0 in .minimize

Demo http://jsfiddle.net/PAj39/2/

0
source

Just set the width of the div: 100%; with position: fixed; to him.

But the wrapper should have a position: absolute; Property

0
source

Source: https://habr.com/ru/post/1438566/


All Articles