How to style the toggle effect? - javascript

I am a newbie to html and CSS. I am designing a website for our conference.I am using the following code to get a toggle effect for the invited speakers of our conference which will give a drop down list box carrying their research interests.
<tr><tr><tr><td><th></th></td></tr></tr></tr><!DOCTYPE html>
<html>
<head>
<style>
.maindrop{
width:50%;
}
.bar{
padding: 20px;
color: white;
background: #1FB5AC;
display: block;
font-family: Times;
text-decoration: none;
font-size: 16px;
}
.bar:hover{
background: gray;
}
.dropbox{
height: 0;
transition: 1s;
overflow: hidden;
width : 100%;
transition-property: background;
transition-timing-function: linear;
}
.dropbox:target{
height:auto;
}
</style>
</head>
<body>
<div class="maindrop">
<div class="fold default">
<a class="bar" href="#tab1">Speaker 1</a>
<div class="dropbox" id='tab1'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.
</div>
</div>
<div class="fold" >
<a class="bar" href="#tab2"> Speaker 2</a>
<div class="dropbox" id='tab2'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.
</div>
</div>
<div class="fold">
<a class="bar" href="#tab3"> Speaker 3</a>
<div class="dropbox" id='tab3'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.
</div>
</div>
<div class="fold">
<a class="bar" href="#tab4"> Speaker 4</a>
<div class="dropbox" id='tab4'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.
</div>
</div>
</div>
</body>
</html>
Quesitons:
First of all the animation is not working properly. The drop down box should come slowly and gently. ( I think I am missing some animation effect!!!)
What I am missing??
Second thing, for the first click drop down box visible, then in the second click one can make the box to reset know. How to do that?
PS: If there any styling option which will make the look better, kindly feel free to edit the code. I will be expecting a pleasant and elegant way of giving information about the speakers of the conference.

.maindrop {
width: 50%;
}
.bar {
padding: 20px;
color: white;
background: #1FB5AC;
display: block;
font-family: Times;
text-decoration: none;
font-size: 16px;
transition: .2s ease-out;
}
.bar:hover {
background: gray;
}
.dropbox {
max-height: 0;
transition: .2s ease-out;
overflow: hidden;
width: 100%;
}
.dropbox:target {
max-height: 300px;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="maindrop">
<div class="fold default">
<a class="bar" href="#tab1">Speaker 1</a>
<div class="dropbox" id='tab1'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.
</div>
</div>
<div class="fold">
<a class="bar" href="#tab2"> Speaker 2</a>
<div class="dropbox" id='tab2'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.
</div>
</div>
<div class="fold">
<a class="bar" href="#tab3"> Speaker 3</a>
<div class="dropbox" id='tab3'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.
</div>
</div>
<div class="fold">
<a class="bar" href="#tab4"> Speaker 4</a>
<div class="dropbox" id='tab4'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.
</div>
</div>
</div>
</body>
</html>

First of all the animation is not working properly.
The main issue you have is that the browser cannot animate between height:0; and height:auto; - the browser needs an explicit start point and an explicit end point to animate between.
So you could (for example) animate between height:0; and height:200px; - but the problem (self-evidently) with that is that not all the Speaker Profiles will be the same length and you don't want to have to manually input different heights for each new profile (including when each profile is edited).
So, the solution is to apply the height value through javascript instead of CSS. Then you can animate between:
dropbox.style.height = '0';
and
dropbox.style.height = dropbox.scrollHeight + 'px';
The second issue you have is that an open dropbox is not closing when clicked on for a second time. This is because you are using the :target pseudo-class to detect the first click... but then if you click for a second time on the same dropbox... the dropbox is still the focus of :target - so nothing changes.
Instead you can add and remove the class .open using javascript. The script will check to see if the fold is open - if it isn't, it will open it, if it isn't, it will close it.
Working example (with Speaker Profiles of different lengths):
var folds = document.getElementsByClassName('fold');
function toggle(fold) {
var dropbox = fold.getElementsByClassName('dropbox')[0];
if (fold.classList.contains('open')) {
dropbox.style.height = '0';
}
else {
dropbox.style.height = dropbox.scrollHeight + 'px';
}
fold.classList.toggle('open');
}
function speakersAccordion() {
var openFolds = document.getElementsByClassName('open');
for (var i = 0; i < openFolds.length; i++) {
if (openFolds[i] === this) continue;
toggle(openFolds[i]);
}
toggle(this);
}
for (var i = 0; i < folds.length; i++) {
folds[i].addEventListener('click',speakersAccordion, false);
}
.maindrop{
width:50%;
}
.bar {
display: block;
padding: 20px;
font-family: Times, serif;
font-size: 16px;
text-decoration: none;
color: white;
background-color: #1FB5AC;
}
.bar:hover {
background-color: gray;
}
.dropbox {
height: 0;
transition: all 0.5s ease-out;
overflow: hidden;
width : 100%;
}
.dropbox p:first-of-type {
margin-top: 0;
}
<div class="maindrop">
<div class="fold default">
<a class="bar" href="#tab1">Speaker 1</a>
<div class="dropbox" id='tab1'>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.</p>
</div>
</div>
<div class="fold" >
<a class="bar" href="#tab2"> Speaker 2</a>
<div class="dropbox" id='tab2'>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.</p>
</div>
</div>
<div class="fold">
<a class="bar" href="#tab3"> Speaker 3</a>
<div class="dropbox" id='tab3'>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis.</p>
</div>
</div>
<div class="fold">
<a class="bar" href="#tab4"> Speaker 4</a>
<div class="dropbox" id='tab4'>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam posuere.</p>
</div>
</div>
</div>

Related

Zoom Out On Page Messes Alignment

I have the following code:
.aligned {
display: flex;
align-items: top;
}
.p {
padding: 15px;
}
img {
border: 5px solid #555;
}
blockquote {
font-family: Georgia, serif;
font-size: 18px;
font-style: italic;
width: 800px;
margin: 0.25em 0;
padding: 0.35em 40px;
line-height: 1.45;
position: relative;
color: #383838;
}
blockquote:before {
display: block;
padding-left: 10px;
content: "\201C";
font-size: 80px;
position: absolute;
left: -20px;
top: -20px;
color: #7a7a7a;
}
blockquote cite {
color: #999999;
font-size: 14px;
display: block;
margin-top: 5px;
}
blockquote cite:before {
content: "\2014 \2009";
}
img {
display: block;
margin: 0 auto;
max-width: 80%;
height: auto;
}
#media all and (min-width: 768px) {
img {
float: left;
height: 200px; /* image height for larger screen only */
}
}
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/Ash_Tree_-_geograph.org.uk_-_590710.jpg/220px-Ash_Tree_-_geograph.org.uk_-_590710.jpg" width="370" height="500">
<div class="aligned">
<div class="p">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut iaculis facilisis consequat. Aenean commodo laoreet mattis. Sed consequat massa magna, quis finibus libero ornare eget. Sed elementum, metus quis elementum porta, lacus dui rhoncus nulla,
a convallis arcu ipsum id erat. Mauris maximus congue mollis. Proin sodales non risus eu porta. Duis iaculis eget quam at suscipit. Aliquam sed erat diam. Nullam dictum urna sit amet vestibulum facilisis. Proin turpis dui, ultrices sit amet tellus
vitae, volutpat tristique turpis. Ut at pretium turpis, ut malesuada mi. Phasellus massa turpis, pretium et diam quis, iaculis sagittis justo.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut iaculis facilisis consequat. Aenean commodo laoreet mattis. Sed consequat massa magna, quis finibus libero ornare eget. Sed elementum, metus quis elementum porta, lacus dui rhoncus nulla,
a convallis arcu ipsum id erat. Mauris maximus congue mollis. Proin sodales non risus eu porta. Duis iaculis eget quam at suscipit. Aliquam sed erat diam. Nullam dictum urna sit amet vestibulum facilisis. Proin turpis dui, ultrices sit amet tellus
vitae, volutpat tristique turpis. Ut at pretium turpis, ut malesuada mi. Phasellus massa turpis, pretium et diam quis, iaculis sagittis justo.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut iaculis facilisis consequat. Aenean commodo laoreet mattis. Sed consequat massa magna, quis finibus libero ornare eget. Sed elementum, metus quis elementum porta, lacus dui rhoncus nulla,
a convallis arcu ipsum id erat. Mauris maximus congue mollis. Proin sodales non risus eu porta. Duis iaculis eget quam at suscipit. Aliquam sed erat diam. Nullam dictum urna sit amet vestibulum facilisis. Proin turpis dui, ultrices sit amet tellus
vitae, volutpat tristique turpis. Ut at pretium turpis, ut malesuada mi. Phasellus massa turpis, pretium et diam quis, iaculis sagittis justo.
</p>
<blockquote>
Nothing Is Impossible. The Word Itself Says 'IM Possible'
<cite>Audrey Hepburn</cite>
</blockquote>
</div>
So when you run the above code, open it on a new page, you can see the alignment to be like this:
On my end, it looks like that but when I zoom out on my website, this is the output I get:
Sorry for the blurred photo
That is what my output looks like when I zoom out. I do not want that, is there a way to make it static, meaning even if I zoom out, the output of the above code I sent does not change?
Even if you run the above code, open it on a new page, and zoom out, you can see the text and the image to change its position. However, I want them to be static, meaning not change at all. Any suggestions?
Update
For a better understanding of what I am looking for, then this is the output I get when I zoom out my page at 33%:
As you can see, the text and image are moved towards the left, which is not what I want. I want them to stay where they were before.
Try wrapping everything inside a dedicated parent div like so:
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/Ash_Tree_-_geograph.org.uk_-_590710.jpg/220px-Ash_Tree_-_geograph.org.uk_-_590710.jpg" width="370" height="500">
<div class="aligned">
<div class="p">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut iaculis facilisis consequat. Aenean commodo laoreet mattis. Sed consequat massa magna, quis finibus libero ornare eget. Sed elementum, metus quis elementum porta, lacus dui rhoncus nulla,
a convallis arcu ipsum id erat. Mauris maximus congue mollis. Proin sodales non risus eu porta. Duis iaculis eget quam at suscipit. Aliquam sed erat diam. Nullam dictum urna sit amet vestibulum facilisis. Proin turpis dui, ultrices sit amet tellus
vitae, volutpat tristique turpis. Ut at pretium turpis, ut malesuada mi. Phasellus massa turpis, pretium et diam quis, iaculis sagittis justo.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut iaculis facilisis consequat. Aenean commodo laoreet mattis. Sed consequat massa magna, quis finibus libero ornare eget. Sed elementum, metus quis elementum porta, lacus dui rhoncus nulla,
a convallis arcu ipsum id erat. Mauris maximus congue mollis. Proin sodales non risus eu porta. Duis iaculis eget quam at suscipit. Aliquam sed erat diam. Nullam dictum urna sit amet vestibulum facilisis. Proin turpis dui, ultrices sit amet tellus
vitae, volutpat tristique turpis. Ut at pretium turpis, ut malesuada mi. Phasellus massa turpis, pretium et diam quis, iaculis sagittis justo.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut iaculis facilisis consequat. Aenean commodo laoreet mattis. Sed consequat massa magna, quis finibus libero ornare eget. Sed elementum, metus quis elementum porta, lacus dui rhoncus nulla,
a convallis arcu ipsum id erat. Mauris maximus congue mollis. Proin sodales non risus eu porta. Duis iaculis eget quam at suscipit. Aliquam sed erat diam. Nullam dictum urna sit amet vestibulum facilisis. Proin turpis dui, ultrices sit amet tellus
vitae, volutpat tristique turpis. Ut at pretium turpis, ut malesuada mi. Phasellus massa turpis, pretium et diam quis, iaculis sagittis justo.
</p>
<blockquote>
Nothing Is Impossible. The Word Itself Says 'IM Possible'
<cite>Audrey Hepburn</cite>
</blockquote>
</div>
</div>
</div>
If this won't work, please make a JSFiddle that illustrates the problem, or share your website address.

Sidebar links not changing when the chapter titles are reaching the top on scroll, they change earlier

I have a sidebar with anchor links that point to a specific content part when I click on them.
The problem appears when I'm scrolling the content, the link in the sidebar gets highlighted when the section title barely appears at the bottom.
How can I make the link change when the title of chapter content reaches the top of sidebar?
Here's what I tried so far but isn't working exactly as I want.
// Sticky sidebar scroll
let stickyLinks = document.getElementById("scrollspy");
let mainNavLinks = document.querySelectorAll("#scrollspy ul li a");
window.addEventListener("scroll", event => {
let fromTop = window.scrollY;
//start from a spe
if(fromTop >= 100){
stickyLinks.classList.add("sticky");
}
else {
stickyLinks.classList.remove("sticky");
}
// add link gray background
mainNavLinks.forEach(link => {
let section = document.querySelector(link.hash);
if (section.offsetTop <= fromTop && section.offsetTop + section.offsetHeight > fromTop) {
link.classList.add("current");
} else {
link.classList.remove("current");
}
});
});
h2 {
font-size: 18px;
}
p {
font-size: 12px;
}
container {
width: 75%;
}
.footer {
background-color: #eee;
text-align: center;
height: 150px;
}
#scrollspy ul {
list-style-type: none;
padding: 0;
border: 1px solid #eee;
border-radius: 0.25rem;
}
#scrollspy ul li a {
text-decoration: none;
padding: 0.5rem 1rem;
display: block;
}
#scrollspy ul li:first-child {
color: #444;
font-size: 0.9rem;
font-weight: bold;
padding: 1rem;
}
#scrollspy ul li a.current {
background: #eee;
}
.sticky {
position: fixed;
top: 10%;
width: 16%;
background-color: white;
z-index: 1;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<div class="container">
<!--some header on top-->
<div class="row startContent">
<div class="col-sm-3">
<h4>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h4>
</div>
<div class="col-sm-9">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat. Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div>
</div>
<!-- content and sidebar -->
<div class="row">
<div class="col-sm-3">
<div id="scrollspy">
<ul>
<li>Contents</li>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</div>
</div>
<div class="col-sm-9">
<div id="chapter1">
<h2>Title of Chapter 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat. Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div> <!--End of chapter 0-->
<div id="chapter2">
<h2>Title of Chapter 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat. Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div> <!--End of chapter 0-->
<div id="chapter3">
<h2>Title of Chapter 3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat. Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div> <!--End of chapter 0-->
<div id="chapter4">
<h2>Title of Chapter 4</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat. Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div> <!--End of chapter 0-->
<div id="chapter5">
<h2>Title of Chapter 5</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat. Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div> <!--End of chapter 0-->
</div>
</div>
</div>
The reason this is happening is because section.offsetTop is returning the position of the section relative to the offset parent. In this case the parent is the div that contains the sections <div class="col-sm-9">.
To fix the problem, we just need to adjust the value for the top of each section to take into account the distance of the parent from the top. We do this as follows:
1. Add an id to the parent container to make it easier to select, e.g.
<div id="chapters-container" class="col-sm-9">
2. Get the offsetTop of the parent so we can add this to the top of the section:
let chaptersTop = document.getElementById("chapters-container").offsetTop;
3. Calculate the actual top position in our scroll listener by adding the distance from the top of the parent container to the position of the section within the parent, which gives us its position from the top of the body in this case. Then we use this when we are checking how far the page is scrolled:
sectionTop = section.offsetTop + chaptersTop;
if (sectionTop <= fromTop && (sectionTop + section.offsetHeight) > fromTop) {
//...
}
Working Example:
(FYI, you aren't closing your li elements in your code, I fixed that here)
// Sticky sidebar scroll
let stickyLinks = document.getElementById("scrollspy");
let mainNavLinks = document.querySelectorAll("#scrollspy ul li a");
let chaptersTop = document.getElementById("chapters-container").offsetTop;
window.addEventListener("scroll", event => {
let fromTop = window.scrollY;
if (fromTop >= 100) {
stickyLinks.classList.add("sticky");
} else {
stickyLinks.classList.remove("sticky");
}
// add link gray background
mainNavLinks.forEach(link => {
let section = document.querySelector(link.hash);
sectionTop = section.offsetTop + chaptersTop;
if (sectionTop <= fromTop && (sectionTop + section.offsetHeight) > fromTop) {
link.classList.add("current");
} else {
link.classList.remove("current");
}
});
});
h2 {
font-size: 18px;
}
p {
font-size: 12px;
}
container {
width: 75%;
}
.footer {
background-color: #eee;
text-align: center;
height: 150px;
}
#scrollspy ul {
list-style-type: none;
padding: 0;
border: 1px solid #eee;
border-radius: 0.25rem;
}
#scrollspy ul li a {
text-decoration: none;
padding: 0.5rem 1rem;
display: block;
}
#scrollspy ul li:first-child {
color: #444;
font-size: 0.9rem;
font-weight: bold;
padding: 1rem;
}
#scrollspy ul li a.current {
background: #eee;
}
.sticky {
position: fixed;
top: 10%;
width: 16%;
background-color: white;
z-index: 1;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<div class="container">
<!--some header on top-->
<div class="row startContent">
<div class="col-sm-3">
<h4>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h4>
</div>
<div class="col-sm-9">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat. Praesent
tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna consectetur eleifend.
Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div>
</div>
<!-- content and sidebar -->
<div class="row">
<div class="col-sm-3">
<div id="scrollspy">
<ul>
<li>Contents</li>
<li>Link 1
</li>
<li>Link 2
</li>
<li>Link 3
</li>
<li>Link 4
</li>
<li>Link 5
</li>
</ul>
</div>
</div>
<div class="col-sm-9" id="chapters-container">
<div id="chapter1">
<h2>Title of Chapter 1
<h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat.
Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna
consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div>
<!--End of chapter 0-->
<div id="chapter2">
<h2>Title of Chapter 2
<h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat.
Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna
consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div>
<!--End of chapter 0-->
<div id="chapter3">
<h2>Title of Chapter 3
<h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat.
Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna
consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div>
<!--End of chapter 0-->
<div id="chapter4">
<h2>Title of Chapter 4
<h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat.
Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna
consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div>
<!--End of chapter 0-->
<div id="chapter5">
<h2>Title of Chapter 5
<h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultricies, lacus eu viverra euismod, arcu dolor condimentum augue, vel congue est ante sit amet purus. Quisque non nunc ut urna gravida porta. Duis sodales vitae tortor in feugiat.
Praesent tellus ante, pharetra sed mauris at, eleifend semper nisl. Donec condimentum purus eget viverra euismod. Ut felis ipsum, sagittis sodales faucibus suscipit, tempus a ante. Integer ac dignissim libero. In tincidunt purus at urna
consectetur eleifend. Nullam hendrerit nulla et enim auctor rhoncus. Pellentesque interdum augue nec augue pulvinar fermentum. Phasellus imperdiet elit id arcu vehicula, ac commodo mauris hendrerit.</p>
</div>
<!--End of chapter 0-->
</div>
</div>
</div>

Floating sticky sidebar is not working as intended

Updated
I have tried my best to reflect my scenario. The sidebar won't show at start, and it can be hidden by a user if desired. The HTML document looks like:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Starter Template for Bootstrap</title>
<link rel="canonical" href="https://getbootstrap.com/docs/4.0/examples/starter-template/">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- Custom styles for this template -->
<style>
.starter-template {
padding: 3rem 1.5rem;
text-align: center;
}
#editUnitBlock{
display: none;
width: 100%;
position: relative !important;
}
#editUnitBlock .ibox-content{
padding: 25px 0px 10px 0px;
border: 0px;
}
.floorStackColSmall {
max-width: 58.333333%;
}
.floorStackColLarge {
max-width: 83.333333%;
}
/*#unitAmenitiesLoading{
position: relative;
}*/
#unitAmenitiesLoading.sticky {
position:fixed;
top:5px;
width: inherit;
max-width: 25%;
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<main role="main" class="container-fluid main-conatiner">
<div class="starter-template">
<div class="row">
<div class="col-md-2">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et augue ut risus tincidunt euismod. Nullam in justo vel nisi suscipit interdum non eu risus. Nullam consectetur justo quis feugiat tristique. Morbi vel massa non erat mollis viverra at pretium erat. Mauris non faucibus lacus. Vivamus pharetra turpis in dui tristique imperdiet. Curabitur fringilla orci ipsum, a porta dolor lobortis venenatis. Suspendisse sodales tellus tempor, dignissim ligula eu, ultricies ante. Phasellus feugiat maximus purus nec ultricies.
Donec semper ipsum in egestas semper. Curabitur gravida libero enim. Aliquam dapibus nulla in tellus molestie, in tincidunt erat accumsan. Donec ornare vitae risus ut condimentum. Donec tincidunt aliquam purus, eget tristique neque hendrerit id. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris mollis lacus at lacinia scelerisque. Suspendisse porttitor rhoncus nulla, at viverra dui suscipit vitae. Sed a metus sed nulla tempus mattis in nec est. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nam consequat elit enim, non sollicitudin justo maximus sed. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aenean aliquet suscipit lectus tristique egestas. Phasellus ipsum orci, congue eu consectetur non, porta et neque. Cras vitae ultricies lorem. Quisque euismod suscipit erat et gravida.
Sed mattis, dolor sed maximus finibus, erat quam aliquam ipsum, et semper odio ante ut odio. Nulla fermentum arcu a tellus maximus, ac auctor justo venenatis. Pellentesque condimentum, lorem non scelerisque maximus, purus sem lobortis quam, sit amet fermentum enim orci pharetra risus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis sed nulla vel nisi auctor convallis. Nulla ut consectetur odio, sit amet pellentesque leo. Vestibulum dapibus pulvinar odio sed malesuada. Quisque molestie tellus eget quam maximus, eget pellentesque eros aliquam.
Vestibulum fringilla, turpis et interdum lacinia, eros felis vulputate turpis, ut pulvinar velit lacus id lorem. Maecenas rutrum, arcu ut ultrices varius, purus nulla scelerisque ante, quis rutrum ante metus in nulla. Aenean bibendum, libero vehicula rutrum tristique, metus turpis tempus nisi, eget elementum nulla ipsum sed justo. Nunc sit amet faucibus leo, nec molestie mauris. Pellentesque sit amet ex posuere, pellentesque dolor et, porttitor ex. Sed faucibus sapien id luctus sodales. Morbi ac efficitur odio. Phasellus non molestie tortor. Sed id arcu ligula. Vestibulum ut maximus ex. Proin faucibus tincidunt tincidunt.
Donec sed pretium ipsum. Praesent nec nisl vel libero lobortis vulputate sit amet nec diam. Mauris tortor erat, egestas ut tincidunt quis, bibendum et lorem. Integer laoreet, nunc quis lobortis iaculis, ipsum sem molestie orci, nec accumsan diam enim eget mi. Mauris porttitor purus eget nulla varius accumsan. Etiam gravida tempus turpis, vel bibendum lorem ullamcorper et. Sed non massa luctus, viverra arcu et, molestie nunc. Praesent eu quam et mauris pellentesque tempus. Etiam lacinia nunc eget mauris pellentesque faucibus. Fusce turpis ante, tincidunt vel sagittis at, pretium vel urna. Ut metus dui, aliquam id eros ac, tristique convallis sem. In ut leo tincidunt, interdum turpis eget, pulvinar neque. Donec nec sagittis quam. Donec nec lorem ex. Sed quis nisl id ex efficitur sollicitudin.
</p>
</div>
<div id="floorStackTable" class="col-md-10 floorStackColLarge">
Show sidebar
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et augue ut risus tincidunt euismod. Nullam in justo vel nisi suscipit interdum non eu risus. Nullam consectetur justo quis feugiat tristique. Morbi vel massa non erat mollis viverra at pretium erat. Mauris non faucibus lacus. Vivamus pharetra turpis in dui tristique imperdiet. Curabitur fringilla orci ipsum, a porta dolor lobortis venenatis. Suspendisse sodales tellus tempor, dignissim ligula eu, ultricies ante. Phasellus feugiat maximus purus nec ultricies.
Donec semper ipsum in egestas semper. Curabitur gravida libero enim. Aliquam dapibus nulla in tellus molestie, in tincidunt erat accumsan. Donec ornare vitae risus ut condimentum. Donec tincidunt aliquam purus, eget tristique neque hendrerit id. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris mollis lacus at lacinia scelerisque. Suspendisse porttitor rhoncus nulla, at viverra dui suscipit vitae. Sed a metus sed nulla tempus mattis in nec est. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nam consequat elit enim, non sollicitudin justo maximus sed. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aenean aliquet suscipit lectus tristique egestas. Phasellus ipsum orci, congue eu consectetur non, porta et neque. Cras vitae ultricies lorem. Quisque euismod suscipit erat et gravida.
Sed mattis, dolor sed maximus finibus, erat quam aliquam ipsum, et semper odio ante ut odio. Nulla fermentum arcu a tellus maximus, ac auctor justo venenatis. Pellentesque condimentum, lorem non scelerisque maximus, purus sem lobortis quam, sit amet fermentum enim orci pharetra risus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis sed nulla vel nisi auctor convallis. Nulla ut consectetur odio, sit amet pellentesque leo. Vestibulum dapibus pulvinar odio sed malesuada. Quisque molestie tellus eget quam maximus, eget pellentesque eros aliquam.
Vestibulum fringilla, turpis et interdum lacinia, eros felis vulputate turpis, ut pulvinar velit lacus id lorem. Maecenas rutrum, arcu ut ultrices varius, purus nulla scelerisque ante, quis rutrum ante metus in nulla. Aenean bibendum, libero vehicula rutrum tristique, metus turpis tempus nisi, eget elementum nulla ipsum sed justo. Nunc sit amet faucibus leo, nec molestie mauris. Pellentesque sit amet ex posuere, pellentesque dolor et, porttitor ex. Sed faucibus sapien id luctus sodales. Morbi ac efficitur odio. Phasellus non molestie tortor. Sed id arcu ligula. Vestibulum ut maximus ex. Proin faucibus tincidunt tincidunt.
Donec sed pretium ipsum. Praesent nec nisl vel libero lobortis vulputate sit amet nec diam. Mauris tortor erat, egestas ut tincidunt quis, bibendum et lorem. Integer laoreet, nunc quis lobortis iaculis, ipsum sem molestie orci, nec accumsan diam enim eget mi. Mauris porttitor purus eget nulla varius accumsan. Etiam gravida tempus turpis, vel bibendum lorem ullamcorper et. Sed non massa luctus, viverra arcu et, molestie nunc. Praesent eu quam et mauris pellentesque tempus. Etiam lacinia nunc eget mauris pellentesque faucibus. Fusce turpis ante, tincidunt vel sagittis at, pretium vel urna. Ut metus dui, aliquam id eros ac, tristique convallis sem. In ut leo tincidunt, interdum turpis eget, pulvinar neque. Donec nec sagittis quam. Donec nec lorem ex. Sed quis nisl id ex efficitur sollicitudin.
</p>
</div>
<div class="col-md-3 " id="editUnitBlock">
<div id="unitAmenitiesLoading" class="ibox-content">
<div class="card">
<div class="card-header">
Featured
</div>
<div class="card-body">
<h5 class="card-title">SIDEBAR TO BE STICKY</h5>
HideSideBar
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio doloremque dicta consequuntur vel asperiores magnam totam, unde sequi rerum beatae excepturi praesentium dolorum nihil a. Provident asperiores alias consequuntur repudiandae.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis consequatur iusto quidem nulla earum eos distinctio, tempore placeat rem, magni officia commodi mollitia qui quasi laborum. Facilis, delectus iure debitis!
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, soluta recusandae commodi esse! Reprehenderit officia facere aliquid ipsa, totam quod illo praesentium eaque nobis, corporis placeat est esse, repellendus perferendis.
</p>
</div>
</div>
</div>
</div>
</div>
<footer id="footer">
<h2>Footer</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Distinctio asperiores suscipit aut quasi obcaecati tempora vel eum, reprehenderit dolores quidem deserunt explicabo quisquam repellendus! Possimus ex, esse in sed illum.
</p>
</footer><br type="_moz">
</div>
</main><!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script>
function editUnit(){
showEditUnitBlock();
}
function showEditUnitBlock()
{
$('#editUnitBlock').show();
// $('#unitAmenitiesLoading').floatit({
// limiter: 'footer',
// preserve_width: true,
// top_spacing: 40,
// bottom_spacing: 10,
// recalculate: true
// });
$('#floorStackTable').addClass("floorStackColSmall").removeClass("floorStackColLarge");
}
function hideEditUnitBlock()
{
$('#editUnitBlock').hide();
$('#floorStackTable').addClass("floorStackColLarge").removeClass("floorStackColSmall");
}
</script>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script type="text/javascript" src="jquery.floatit.js"></script>
<script>
$(function(){
// $('#unitAmenitiesLoading').floatit({
// limiter: 'footer',
// preserve_width: true,
// top_spacing: 40,
// bottom_spacing: 10,
// recalculate: true
// });
// Cache our vars for the fixed sidebar on scroll
var $sidebar = $('#unitAmenitiesLoading');
// Get & Store the original top of our #sidebar-nav so we can test against it
var sidebarTop = $sidebar.position().top;
var $footer = $('#footer');
var footerTop = $footer.position().top;
// Edit the `- 10` to control when it should disappear when the footer is hit.
var blogHeight = $('#floorStackTable').outerHeight() - 10;
console.log(blogHeight);
console.log(sidebarTop);
console.log(footerTop);
// Add the function below to the scroll event
$(window).scroll(fixSidebarOnScroll);
// On window scroll, this fn is called (binded above)
function fixSidebarOnScroll(){
// Cache our scroll top position (our current scroll position)
var windowScrollTop = $(window).scrollTop();
console.log(windowScrollTop);
// Add or remove our sticky class on these conditions
if (windowScrollTop >= blogHeight || windowScrollTop <= sidebarTop || window.innerHeight + window.scrollY > blogHeight){
// Remove when the scroll is greater than our #content.OuterHeight()
// or when our sticky scroll is above the original position of the sidebar
$sidebar.removeClass('sticky');
}
// Scroll is past the original position of sidebar
else if (windowScrollTop >= sidebarTop){
// Otherwise add the sticky if $sidebar doesnt have it already!
if (!$sidebar.hasClass('sticky')){
$sidebar.addClass('sticky');
}
}
}
})
</script>
</body>
</html>
The sidebar seems to be fixed, but only whenever we scroll right to the footer. I would like the side-nav to be scrolled up instead of directly being moved to its relative position.
Please, check the code available at jsfiddle to help me to solve this issue. By the way, i am open to solving this by using any other jquery plugin or even with the custom script as well.
I am quite new to the JQuery Framework, if any one can help me by correcting the jsfiddle code it would be appriciated.
Thank You.
Please, let me know if there is anything I can do to further clarify this issue I am attempting to resolve.
Update: 2.0
I am looking something like http://webpop.github.io/jquery.pin/, here you can see that a box got pinned on the sidebar, it remained fixed upto certain scroll point then start scrolling thereafter.
You could use CSS's position: sticky; instead. This way you don't need to worry about adding a class at a certain point either.
https://jsfiddle.net/htbLm8rw/
in such cases, mostly I use "Simple sticky Sidebar" jQuery plugin. Simple Sticky Sidebar code & documentation can be found here https://github.com/ismailfarooq/simple-sticky-sidebar
See below example:
.text{
padding-bottom: 50px;
padding-top: 20px;
border-bottom: 1px solid #ccc;
}
.footer{
background: #333;
color: #fff;
padding: 30px 0;
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div class="container">
<header class="main-header">
<div class="container"><h3>Simple Sticky Sidebar</h3></div>
</header>
<div class="area"><h1>Simple Sticky Sidebar</h1></div>
<div class="row">
<div class="col-lg-3 col-sm-3">
<div class="sidebar">
<div class="sidebar-inner">
<div class="text"> <h2> Sidebar Content</h2></div>
<div class="text">Sidebar Content</div>
<div class="text">Sidebar Content</div>
<div class="text">Sidebar Content</div>
<div class="text">Sidebar Content</div>
<div class="text">Sidebar Content</div>
<div class="text">Sidebar Content</div>
<div class="text">Sidebar Content</div>
<div class="text">Sidebar Content</div>
</div>
</div>
</div>
<div class="col-lg-9 col-sm-9">
<div class="search-content">
<div class="content">
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p> </div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text">content</div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p> </div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text">content</div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p> </div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text">content</div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
<div class="text"><h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam id justo eu ligula laoreet sodales ut nec velit. Quisque auctor, metus a pharetra rhoncus, lorem massa scelerisque dolor, nec varius odio nulla sit amet est. Duis tempus sapien at tincidunt gravida. Suspendisse vitae malesuada eros, sed ullamcorper ante. Vivamus eget condimentum odio, sed feugiat tortor. Vestibulum accumsan, sem a auctor tincidunt, metus dui sagittis eros, eget interdum odio sem id eros. Nullam non sem at justo eleifend fermentum at vel felis. </p></div>
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<h2> #FOOTER Content </h2> <br/>
#FOOTER<br/>
#FOOTER<br/>
#FOOTER<br/>
#FOOTER<br/>
#FOOTER<br/>
</div>
<!--
<script type="text/javascript" src="js/simple-sticky-sidebar.js"></script>
-->
<!-- Uncomment this script and add Simple Sticky Sidebar JS from your local. You can download Simple Sticky Sidebar from Github Repo -->
<!-- Remove below when you are working on local -->
<script>
/* ========================================================================
* Simple Sticky Sidebar
* #version 0.1
* #author Ismail Farooq <ismail_farooq#yahoo.com>
* #license The MIT License (MIT) (https://github.com/ismailfarooq/simple-sticky-sidebar/blob/master/LICENSE)
* ======================================================================== */
function setStyle(element, cssProperty) {
for (var property in cssProperty){
element.style[property] = cssProperty[property];
}
}
function destroySticky(element){
setStyle(element, {
top : '',
left : '',
bottom : '',
width : '',
position : ''
});
}
function getOffset(el) {
el = el.getBoundingClientRect();
return {
left: el.left + window.scrollX,
top: el.top + window.scrollY
}
}
function simpleStickySidebar(element, options) {
// Global options
var sticky = document.querySelector(element); // Sticky sidebar
var container = document.querySelector(options.container); // Sticky sidebar container
var topSpace = options.topSpace ? options.topSpace : 0; // Top spacing after sticky
var bottomSpace = options.bottomSpace ? options.bottomSpace : 0; // Bottom spacing after sticky
// vars
var $window = window; // window
var stickyHeight = sticky.getBoundingClientRect().height; // Sticky sidebar height
var stickyOffsetTop = getOffset(sticky).top; // Sticky sidebar top offset
var stickyOffsetBottom = getOffset(sticky).top + sticky.getBoundingClientRect().height; // Sticky sidebar bottom offset
var stickyOffsetLeft = getOffset(sticky).left; // Sticky sidebar left offset
var topFixed = false; // checkpoint
var bottomFixed = false; // checkpoint
var lastScrollVal = 0; // checkpoint
var getStickyHeight = function() {
return document.querySelector(element).getBoundingClientRect().height;
};
// scrolling
window.addEventListener('scroll', function(event) {
var scrollTop = window.scrollY;
// when scroll position touch the "Sidebar"
if(scrollTop > stickyOffsetTop - topSpace){
// if "Sidebar" smaller than viewport
if(getStickyHeight() <= $window.innerHeight - topSpace){
// fix "Sidebar" from top
setStyle(sticky, {
top : topSpace + "px",
left : stickyOffsetLeft + "px",
bottom : '',
width : sticky.getBoundingClientRect().width + "px",
position : 'fixed'
});
}
else {
// scrolling down
if (scrollTop > lastScrollVal){
// if "Sidebar" fixed from top during scrolling down
if(topFixed){
// get new offset of "Sidebar"
var absoluteStickyOffsetTop = getOffset(sticky).top;
setStyle(sticky, {
top : absoluteStickyOffsetTop - getOffset(container).top + "px",
left : '',
bottom : '',
width : '',
position : 'absolute'
});
topFixed = false;
}
// make "Sidebar" fixed from bottom when bottom area visible in viewport
if(scrollTop > stickyOffsetBottom - $window.innerHeight){
setStyle(sticky, {
top : '',
left : stickyOffsetLeft + "px",
bottom : bottomSpace + "px",
width : sticky.getBoundingClientRect().width + "px",
position : 'fixed'
});
bottomFixed = true;
}
} else { // scrolling up
// get new offset of "Sidebar" during scrolling up
var absoluteStickyOffsetTop = getOffset(sticky).top;
// if "Sidebar" fixed from bottom, stop sticky to its position
if(bottomFixed){
setStyle(sticky, {
top : absoluteStickyOffsetTop - getOffset(container).top + "px",
left : '',
bottom : '',
width : '',
position : 'absolute'
});
bottomFixed = false;
}
// make "Sidebar" fixed from top when top area visible in viewport
if(scrollTop < absoluteStickyOffsetTop - topSpace){
setStyle(sticky, {
top : topSpace + "px",
left : stickyOffsetLeft + "px",
bottom : '',
width : sticky.getBoundingClientRect().width + "px",
position : 'fixed'
});
topFixed = true;
}
}
lastScrollVal = scrollTop;
}
} else {
// make sidebar to default position
destroySticky(sticky);
}
});
}
</script>
<!-- Remove above -->
<script type="text/javascript">
simpleStickySidebar('.sidebar-inner', {
container: '.sidebar',
topSpace: document.querySelector('.main-header').getBoundingClientRect().height + 20, // or any no i.e 20
bottomSpace : document.querySelector('.footer').getBoundingClientRect().height + 20
});
</script>
Add a div inside the .col class which has to have "sticky" functionality. Give that div a class to style it with and set position: sticky on that div.
This wil allow the new div to become sticky or static based on the room in the column of the parent.
You can set the trigger offsets with top, left, right and bottom.
Checkout the snippet to see it in action.
$(function() {
// $('#unitAmenitiesLoading').floatit({
// limiter: 'footer',
// preserve_width: true,
// top_spacing: 40,
// bottom_spacing: 10,
// recalculate: true
// });
// Cache our vars for the fixed sidebar on scroll
var $sidebar = $('#unitAmenitiesLoading');
// Get & Store the original top of our #sidebar-nav so we can test against it
var sidebarTop = $sidebar.position().top;
var $footer = $('#footer');
var footerTop = $footer.position().top;
// Edit the `- 10` to control when it should disappear when the footer is hit.
var blogHeight = $('#floorStackTable').outerHeight() - 10;
// console.log(blogHeight);
// console.log(sidebarTop);
// console.log(footerTop);
// Add the function below to the scroll event
$(window).scroll(fixSidebarOnScroll);
// On window scroll, this fn is called (binded above)
function fixSidebarOnScroll() {
// Cache our scroll top position (our current scroll position)
var windowScrollTop = $(window).scrollTop();
//console.log(windowScrollTop);
// Add or remove our sticky class on these conditions
if (windowScrollTop >= blogHeight || windowScrollTop <= sidebarTop || window.innerHeight + window.scrollY > blogHeight) {
// Remove when the scroll is greater than our #content.OuterHeight()
// or when our sticky scroll is above the original position of the sidebar
$sidebar.removeClass('sticky');
}
// Scroll is past the original position of sidebar
else if (windowScrollTop >= sidebarTop) {
// Otherwise add the sticky if $sidebar doesnt have it already!
if (!$sidebar.hasClass('sticky')) {
$sidebar.addClass('sticky');
}
}
}
})
.sticky {
position: -webkit-sticky;
position: sticky;
top: 50px;
bottom: 50px;
}
.starter-template {
padding: 3rem 1.5rem;
text-align: center;
}
#editUnitBlock {
display: none;
width: 100%;
position: relative !important;
}
#editUnitBlock .ibox-content {
padding: 25px 0px 10px 0px;
border: 0px;
}
.floorStackColSmall {
max-width: 58.333333%;
}
.floorStackColLarge {
max-width: 83.333333%;
}
/*#unitAmenitiesLoading{
position: relative;
}*/
#unitAmenitiesLoading.sticky {
position: fixed;
top: 5px;
width: inherit;
max-width: 25%;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Starter Template for Bootstrap</title>
<link rel="canonical" href="https://getbootstrap.com/docs/4.0/examples/starter-template/">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<main role="main" class="container-fluid main-conatiner">
<div class="starter-template">
<div class="row">
<div class="col-md-2">
<!-- Added sticky here -->
<div class="sticky">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et augue ut risus tincidunt euismod. Nullam in justo vel nisi suscipit interdum non eu risus. Nullam consectetur justo quis feugiat tristique. Morbi vel massa non erat mollis viverra
at pretium erat.
</p>
</div>
<!-- -->
</div>
<div id="floorStackTable" class="col-md-10 floorStackColLarge">
Show sidebar
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et augue ut risus tincidunt euismod. Nullam in justo vel nisi suscipit interdum non eu risus. Nullam consectetur justo quis feugiat tristique. Morbi vel massa non erat mollis viverra at pretium
erat. Mauris non faucibus lacus. Vivamus pharetra turpis in dui tristique imperdiet. Curabitur fringilla orci ipsum, a porta dolor lobortis venenatis. Suspendisse sodales tellus tempor, dignissim ligula eu, ultricies ante. Phasellus feugiat
maximus purus nec ultricies. Donec semper ipsum in egestas semper. Curabitur gravida libero enim. Aliquam dapibus nulla in tellus molestie, in tincidunt erat accumsan. Donec ornare vitae risus ut condimentum. Donec tincidunt aliquam purus,
eget tristique neque hendrerit id. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris mollis lacus at lacinia scelerisque. Suspendisse porttitor rhoncus nulla, at viverra dui suscipit vitae. Sed
a metus sed nulla tempus mattis in nec est. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nam consequat elit enim, non sollicitudin justo maximus sed. Class aptent taciti sociosqu ad litora torquent
per conubia nostra, per inceptos himenaeos. Aenean aliquet suscipit lectus tristique egestas. Phasellus ipsum orci, congue eu consectetur non, porta et neque. Cras vitae ultricies lorem. Quisque euismod suscipit erat et gravida. Sed mattis,
dolor sed maximus finibus, erat quam aliquam ipsum, et semper odio ante ut odio. Nulla fermentum arcu a tellus maximus, ac auctor justo venenatis. Pellentesque condimentum, lorem non scelerisque maximus, purus sem lobortis quam, sit amet fermentum
enim orci pharetra risus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis sed nulla vel nisi auctor convallis. Nulla ut consectetur odio, sit amet pellentesque leo. Vestibulum dapibus pulvinar odio
sed malesuada. Quisque molestie tellus eget quam maximus, eget pellentesque eros aliquam. Vestibulum fringilla, turpis et interdum lacinia, eros felis vulputate turpis, ut pulvinar velit lacus id lorem. Maecenas rutrum, arcu ut ultrices varius,
purus nulla scelerisque ante, quis rutrum ante metus in nulla. Aenean bibendum, libero vehicula rutrum tristique, metus turpis tempus nisi, eget elementum nulla ipsum sed justo. Nunc sit amet faucibus leo, nec molestie mauris. Pellentesque
sit amet ex posuere, pellentesque dolor et, porttitor ex. Sed faucibus sapien id luctus sodales. Morbi ac efficitur odio. Phasellus non molestie tortor. Sed id arcu ligula. Vestibulum ut maximus ex. Proin faucibus tincidunt tincidunt. Donec
sed pretium ipsum. Praesent nec nisl vel libero lobortis vulputate sit amet nec diam. Mauris tortor erat, egestas ut tincidunt quis, bibendum et lorem. Integer laoreet, nunc quis lobortis iaculis, ipsum sem molestie orci, nec accumsan diam
enim eget mi. Mauris porttitor purus eget nulla varius accumsan. Etiam gravida tempus turpis, vel bibendum lorem ullamcorper et. Sed non massa luctus, viverra arcu et, molestie nunc. Praesent eu quam et mauris pellentesque tempus. Etiam lacinia
nunc eget mauris pellentesque faucibus. Fusce turpis ante, tincidunt vel sagittis at, pretium vel urna. Ut metus dui, aliquam id eros ac, tristique convallis sem. In ut leo tincidunt, interdum turpis eget, pulvinar neque. Donec nec sagittis
quam. Donec nec lorem ex. Sed quis nisl id ex efficitur sollicitudin.
</p>
</div>
<div class="col-md-3 " id="editUnitBlock">
<div id="unitAmenitiesLoading" class="ibox-content">
<div class="card">
<div class="card-header">
Featured
</div>
<div class="card-body">
<h5 class="card-title">SIDEBAR TO BE STICKY</h5>
HideSideBar
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio doloremque dicta consequuntur vel asperiores magnam totam, unde sequi rerum beatae excepturi praesentium dolorum nihil a. Provident asperiores alias consequuntur repudiandae.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis consequatur iusto quidem nulla earum eos distinctio, tempore placeat rem, magni officia commodi mollitia qui quasi laborum. Facilis, delectus iure debitis!
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, soluta recusandae commodi esse! Reprehenderit officia facere aliquid ipsa, totam quod illo praesentium eaque nobis, corporis placeat est esse, repellendus perferendis.
</p>
</div>
</div>
</div>
</div>
</div>
<footer id="footer">
<h2>Footer</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Distinctio asperiores suscipit aut quasi obcaecati tempora vel eum, reprehenderit dolores quidem deserunt explicabo quisquam repellendus! Possimus ex, esse in sed illum.
</p>
</footer><br type="_moz">
</div>
</main>
<!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script>
function editUnit() {
showEditUnitBlock();
}
function showEditUnitBlock() {
$('#editUnitBlock').show();
// $('#unitAmenitiesLoading').floatit({
// limiter: 'footer',
// preserve_width: true,
// top_spacing: 40,
// bottom_spacing: 10,
// recalculate: true
// });
$('#floorStackTable').addClass("floorStackColSmall").removeClass("floorStackColLarge");
}
function hideEditUnitBlock() {
$('#editUnitBlock').hide();
$('#floorStackTable').addClass("floorStackColLarge").removeClass("floorStackColSmall");
}
</script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script type="text/javascript" src="jquery.floatit.js"></script>
</body>
</html>

CSS Text Truncation Vertical Alignment

I have a lorem ipsum text that is truncated. When you press the "readmore »" button it fully displays the text.
My issue is the text shifts up vertically when you press the "readmore »" button.
$(document).ready(function(){
$(".readmore-btn").on("click",function(){
$(".readmore").toggleClass("truncate");
})
});
.truncate {
max-height: 55px;
overflow-y:hidden;
}
.readmore-btn {
color: #337ab7;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Text before</h1>
<div class="readmore truncate">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vel lacus est. Ut vulputate venenatis mauris porta ornare. Nunc vehicula dapibus sem eget maximus. Vestibulum congue nunc eget feugiat euismod. Praesent porta cursus libero, ut vestibulum leo suscipit non. Vivamus vulputate urna quis tincidunt congue. </p>
</div>
<div class="readmore-btn">Readmore »</div>
<h4>Text After</h1>
Moving the overflow-y: hidden; from the .truncate class and moving it to the .readmore class resolves the shifting issue.
$(document).ready(function(){
$(".readmore-btn").on("click",function(){
$(".readmore").toggleClass("truncate");
})
});
.readmore {
overflow-y:hidden;
}
.truncate {
max-height: 55px;
}
.readmore-btn {
color: #337ab7;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Text before</h1>
<div class="readmore truncate">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vel lacus est. Ut vulputate venenatis mauris porta ornare. Nunc vehicula dapibus sem eget maximus. Vestibulum congue nunc eget feugiat euismod. Praesent porta cursus libero, ut vestibulum leo suscipit non. Vivamus vulputate urna quis tincidunt congue. Vestibulum malesuada neque sit amet eros efficitur pharetra. Vestibulum consequat purus sit amet ex euismod, ut consequat tellus vehicula. Proin suscipit in risus ut placerat. Integer porta id libero ac finibus. Ut at tellus turpis. Phasellus dolor erat, ullamcorper a risus ultrices, iaculis malesuada nisl. Integer tellus arcu, sodales quis mattis ac, sagittis eu turpis. Suspendisse tristique tempor pharetra.</p>
</div>
<div class="readmore-btn">Readmore »</div>
<h4>Text After</h1>
This is caused by the default margins the browsers adds to paragraphs.
Just add
.truncate p{margin:0}
to your css

how can I have multiple tabbed boxes on one page

I am struggling to get multiple instances of this tabbed box on one page. I am very new to jquery, and the code I have is copied. The tabbed box works fine, but multiple instances cause problems.
The HTML:
<ul class="tabs">
<li class="active" rel="tab1"> Single Wall Cases</li>
<li rel="tab2">Double Wall Cases</li>
<li rel="tab3">Heavy Duty Cases</li>
<li rel="tab4">Wardrobe Cartons</li>
<li rel="tab5">Archive Boxes</li>
<li rel="tab6">Stitched Wallets</li>
</ul>
<div class="tab_container">
<div id="tab1" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab1 -->
<div id="tab2" class="tab_content">
<p><img src="../images/web size/Pillow Pak'R 1web.jpg" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab2 -->
<div id="tab3" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab3 -->
<div id="tab4" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab4 -->
<div id="tab5" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab5 -->
<div id="tab6" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab6 -->
The CSS:
ul.tabs {
margin-top: 0;
margin-right: 0;
margin-left: 0;
margin-bottom: 0;
padding: 0;
float: left;
list-style: none;
height: 32px;
/* [disabled]border-bottom: 1px solid #999999; */
/* [disabled]border-left: 1px solid #999999; */
width: 100%;
}
ul.tabs li {
margin-top: 0;
margin-right: 0;
margin-left: 2px;
margin-bottom: 0;
cursor: pointer;
padding: 0px 21px;
height: 31px;
line-height: 31px;
border-color: rgba(37,37,37,1.00);
border-left-style: solid;
/* [disabled]border-right-style: solid; */
/* [disabled]border-bottom-style: solid; */
/* [disabled]border-top-style: solid; */
border-width: 1px;
overflow: hidden;
position: relative;
font-family: abel;
font-style: normal;
font-weight: 400;
color: rgba(37,37,37,1.00);
float: left;
}
ul.tabs li:hover {
background: #CCCCCC;
color: rgba(37,37,37,1.00);
}
ul.tabs li.active{
background-color: #8DDF2D;
border-bottom: 1px solid #FFFFFF;
color: rgba(37,37,37,1.00);
}
.tab_container {
border: 1px solid #999999;
clear: both;
float: left;
width: 100%;
background: #FFFFFF;
}
.tab_content {
padding: 20px;
font-size: 1.2em;
display: none;
}
#media (max-width: 600px){
ul.tabs li {
padding-top: 0px;
padding-right: 2px;
padding-left: 2px;
padding-bottom: 0px;
margin: 0;
font-size: 77%;
float: none;
width: 92px;
}
ul.tabs {
}
.tab_container {
margin-top: 175px;
}
And the Jquery:
<script type="text/javascript">
$(document).ready(function() {
$(".tab_content").hide();
$(".tab_content:first").show();
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".tab_content").hide();
var activeTab = $(this).attr("rel");
$("#"+activeTab).fadeIn();
});
});
</script>
See the jsfiddle!
I have multiple section tags on my page, and would like this tabbed box in each section.
Here is a working example of the Tabs you wish to have. Check out the code snippet. I hope that is what you are looking for.
$(document).ready(function() {
$(".tab_content").hide();
$(".tab_content:first").show();
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".tab_content").hide();
var activeTab = $(this).attr("rel");
$("#"+activeTab).fadeIn();
});
});
ul.tabs {
margin-top: 0;
margin-right: 0;
margin-left: 0;
margin-bottom: 0;
padding: 0;
float: left;
list-style: none;
height: 32px;
/* [disabled]border-bottom: 1px solid #999999; */
/* [disabled]border-left: 1px solid #999999; */
width: 100%;
}
ul.tabs li {
margin-top: 0;
margin-right: 0;
margin-left: 2px;
margin-bottom: 0;
cursor: pointer;
padding: 0px 21px;
height: 31px;
line-height: 31px;
display:inline;
border-color: rgba(37,37,37,1.00);
border-left-style: solid;
/* [disabled]border-right-style: solid; */
/* [disabled]border-bottom-style: solid; */
/* [disabled]border-top-style: solid; */
border-width: 1px;
overflow: hidden;
position: relative;
font-family: abel;
font-style: normal;
font-weight: 400;
color: rgba(37,37,37,1.00);
float: left;
}
ul.tabs li:hover {
background: #CCCCCC;
color: rgba(37,37,37,1.00);
}
ul.tabs li.active{
background-color: #8DDF2D;
border-bottom: 1px solid #FFFFFF;
color: rgba(37,37,37,1.00);
}
.tab_container {
border: 1px solid #999999;
clear: both;
float: left;
width: 100%;
background: #FFFFFF;
}
.tab_content {
padding: 20px;
font-size: 1.2em;
display: none;
}
#media (max-width: 600px){
ul.tabs li {
padding-top: 0px;
padding-right: 2px;
padding-left: 2px;
padding-bottom: 0px;
margin: 0;
font-size: 77%;
float: none;
width: 92px;
}
ul.tabs {
}
.tab_container {
margin-top: 175px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="tabs">
<li class="active" rel="tab1"> Single Wall Cases</li>
<li rel="tab2">Double Wall Cases</li>
<li rel="tab3">Heavy Duty Cases</li>
<li rel="tab4">Wardrobe Cartons</li>
<li rel="tab5">Archive Boxes</li>
<li rel="tab6">Stitched Wallets</li>
</ul>
<div class="tab_container">
<div id="tab1" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab1 -->
<div id="tab2" class="tab_content">
<p><img src="../images/web size/Pillow Pak'R 1web.jpg" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab2 -->
<div id="tab3" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab3 -->
<div id="tab4" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab4 -->
<div id="tab5" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab5 -->
<div id="tab6" class="tab_content">
<p><img src="../images/round images/boxes1.png" width="200"> <br />
<strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ultrices vel felis id pulvinar. Aliquam vulputate auctor luctus. Etiam nisl lectus, tempor eu justo sit amet, vestibulum viverra ante. Mauris sed condimentum tortor. Duis sed pharetra turpis. Aliquam pulvinar ante quam, ut lobortis dolor faucibus rhoncus. Nullam quis velit nunc. Phasellus aliquam, diam nec finibus blandit, ligula est molestie risus, iaculis pellentesque ipsum magna ut neque. Duis tempus, justo non rhoncus rhoncus, nibh ligula sagittis enim, in pellentesque metus enim at eros. Aenean sit amet ipsum vitae sem tempus fermentum. Vivamus pharetra, metus id hendrerit ultrices, nisl elit congue nunc, non gravida ante mi ac nisl. Cras viverra luctus neque. Cras et dolor imperdiet, dapibus velit sit amet, feugiat nunc. Curabitur iaculis felis vitae convallis ornare.
</strong>
</p>
</div><!-- #tab6 -->
The issue was with display attribute in css

Categories