Stick deep nested element - javascript

I have a very deeply nested <div> element. How to render a new element inside this element that will float at the top of the screen (body or html)?
Sticky position is relative to the closest element (parent) and this is not an answer.
If it can not be done with CSS, is there any JS library that can do it?
<html>
<body>
<div>
<div>element should be sticky to the top of the page</div>
</div>
</body>
</html>

Add a class to a div and give position fixed and top 0 to get the element in the top part of screen.
Also, in this example, I gave body 200vh in order to scroll and see the element fixed on top.
body {
height: 200vh;
}
.sticky {
position: fixed;
top: 0;
}
<html>
<body>
<div>
<div class="sticky">element should be sticky to the top of the page</div>
</div>
</body>
</html>

Related

jquery multiple append avoid increasing window size

I have been working on the next example, I'm adding html to a div, at some point the size of the window start increasing, is there a way to avoid this?
example
$(document).ready(function() {
$('#b').click(function() {
$('#elDiv').append('<br><strong>Hello</strong><br>');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>I would like to say: </p>
<button id="b">click now</button>
<div id="elDiv"></div>
I defined a style for the elDiv to be 100px height and scroll when content exceeds the div's height:
.myclass{
height: 100px;
overflow: auto;
}
Updated fiddle: https://jsfiddle.net/9rysbxw2/18/
to avoid this behavio i think you should set a size to the element and define a overflow behavior. So the content that is been added to the div will stay inside it. here is a modification of you code sample:
$('#b').click(function(){
$( '#elDiv').append('<br><strong>Hello</strong><br>');
});
<p>I would like to say: </p>
<button id="b">click now
</button>
<div id="elDiv" style="height:100px; overflow: auto;">
</div>
<!-- NOTICE THE OVERFLOW PROPERTIE I ADDED AND THE FIXED HEIGHT -->
i resolved the issue using the style properties
position: absolute;
top: 20px;
this way i can add multiple div at any position without increasing the screen size
example

Drag and Drop Angular File Upload with Relatively Positioned Elements

I am having an issue with Angular File upload, being used with relatively positioned elements. My drop target is 100% width and height, absolutely positioned. If you drag the file over any non-relatively positioned element, the overlay appears fine and everything works. However if you drag over a relatively positioned element, it does not register the drag event. This is because the relative positioned elements are appearing on top of the dropArea.
I've tried applying a z-index to the drop target, and the drag and drop works great, but then I cannot click anything on the UI anymore.
Here is my logic:
HTML
<html>
<head>...</head>
<body>
<div id="dropArea">...</div>
<div id="siteContent">
<div class="row">
<!-- dragging to this element fails, since it is relatively positioned -->
<div class="col-md-12">...</div>
<div>...</div>
</div>
</div>
</body>
</html>
CSS
#dropArea {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
Is there any way to apply a z-index to the dropArea but still allow clicks to pass through?
I solved this by wrapping the entire page content in the drop area.
<html>
<head>...</head>
<body>
<div id="dropArea" class="dropArea" ng-file-drop="onFileSelect($files)" ng-file-drag-over-class="fileAdded">
<div id="drop-content-container">
<div id="drop-content">
<img src="img/app/files-upload-dd.png">
<h1>Drop Files Here!</h1>
</div>
</div>
<div class="page-content">...</div>
</div>
</body>
</html>

Is it possible to re-position a position:absolute DIV?

The scenario:-
A published HTML page has position:absolute DIVs and all DIV heights are set to specific px values. The page is editable via an online CMS such as Surreal or Cushy. The editor enters more content that the DIV was designed to take. The result is that the extra content overflows the DIV and the page design is trashed.
Is there any way that when an editor does this that the DIV height expands AND all other DIVs on the page move down? Bare in mind that the DIV heights cannot be set to 100% but have fixed px values.
I am assuming the solution maybe jQuery or JavaScript - any ideas?
<body>
<div id="two" style="position:absolute;left:163px;top:0px;width:738px;height:269px;z-index:5;padding:0;">
<img src="images/two.jpg" id="two" alt="two" border="0" title="two" style="width:738px;height:269px;">
</div>
<div id="three" style="position:absolute;left:0px;top:350px;width:900px;height:294px;z-index:6;" class="editable">
<!-- div content -->
<!-- this is where the user/editor will add content -->
</div>
<div id="four" style="position:absolute;left:0px;top:0px;width:900px;height:323px;z-index:7;padding:0;">
<div id="five" style="position:absolute;left:0px;top:0px;width:162px;height:269px;z-index:0;padding:0;">
<img src="logo.gif" id="logo" alt="Logo" border="0" title="Logo" style="width:162px;height:269px;">
</div>
I don't see exactly the scenario, but have you considered the scroll within your fixed size divs ?
Give a class to those divs, such as
<div class="bescrollable"></div>
and then in your css :
.bescrollable {overflow:auto;}
scrollbars will be added when overflows occur
You can set height of the div according to the content like this:
.container {
position: absolute;
width: 400px;
height: 400px;
}
.content {
width: 100%;
height: 100%;
}
<div class="container">
<div class="content">...</div>
</div>
$('.container').css('height', $('.content').height());
Here a fiddle: http://jsfiddle.net/Kdktw/
As CBRRacer mentioned in the comments, if we could see the HTML, the answer would be more accurate to your situation.
I hope this helps!
This would be predicated on the height of the content within the box. Probably the best method would be to have the height of the content div set to auto and use javascript to get the height of the element.
// Using jQuery
var contentDivHeight = $('#myContentDiv').height();
var startingOffset = 250; // The height of the div original set at startup
Then you could simply add this height to each of the primary display controls that would have to be moved "down". If you assigned them all a common class they could all easily be selected (such "primaryInterface" or something).
$(document).ready(function() {
$('.primaryInterface')each(function (i) {
var newTop = this.offset().top + contentDivHeight - startingOffset;
var newLeft = this.offset().left;
this.offset({ top: newTop, left: newleft });
});
});
This code is untested, but ideally, once the page loads, it would find all of the elements with the specified class and set their top offset to be the difference between the starting height of the div and the resultant height.

Get height of a div with nested absolute divs with js

I've got a a div acting as a container which is positioned as relative.
Within this div I have 3 other divs positioned as absolute.
<head>
<style>
#container{ position:relative; }
#block1, #block2, #block3 { position:absolute; }
#block2 { top:100px; }
#block3 { top:600px; }
</style>
</head>
<div id="container">
<div id="block1"> some text </div>
<div id="block2"> some text </div>
<div id="block3"> some text </div>
</div>
How can I get correct height of a relative div with js.
I tried .clientHeight and jquery .height() but won't work.
Thanks!
I think you got something wrong. When you position divs inside other divs like that, the container-div will have nearly no (or no) height at all. That's cause of the absolute positioned divs. They can be placed "outside" the container-div, and only use it as a reference for x and y offsets.

Enable scrolling on a single div?

I have a layout that looks like this:
<html>
<head>
stuff here
</head>
<body>
<div id="master">
<div id="toolbar">
<input type="text" id="foo">
</div>
<div id="content">
a whole bunch of content in here
</div>
</div>
</body>
</html>
'#master' is a container for a jquery UI dialog. I'd like the contents of the '#content' div to be scrollable but for '#toolbar' to NOT be scrollable. Is this doable with jquery UI's dialog?
Just use css rules:
#content { overflow: auto; height: [desired height] }
Where you might need to use jQuery is if the modal has a dynamic height. In that case on open of the modal you could set the inner container height. Something like:
open: function(){
var modalHeight = $('#master').height();
$('#content').height(modalHeight - [footer and title]);
}
Use CSS to give the #content div a set height and optionally a width, and set the CSS property overflow: auto on that div as well. If the content exceeds the height, you'll get a scrollbar.

Categories