How to use Scrollspy with bootstrap 4?
When scroll fixed left sidebar and then go to the footer the section then fixed from the footer content.
The main concept for the fixing left sidebar then we need to
get height from the main page container and also, get the height of the footer and us
need to fix from the bottom when getting new height otherwise need to fixed from
the top position with the data content. I think you will be more understanding
when you will be use code as below example:
var footerBottomHeight = pageHeight - footerHeight;
var bottomHeight = foo terBottomHeight - dataContentH eight;
Scrollspy example with bootstrap 4.
<!DOCTYPE html>
<html>
<head>
<title>Untitled</ title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to- fit=no">
<link rel="stylesheet" hre f="https://maxcdn. bootstrapcdn.com/bootstrap/4. 3.1/css/bootstrap.min.css">
<style>
header{height: 200 px; background: #0bf; color: # fff; text-align: center;}
.fixedClass{positi on: fixed; width: 250px;}
footer{height: 400 px; background: #eee; border- top: 1px solid #ccc;}
</style>
</head>
<body data-spy="scroll" data- target="#list-example">
<header>
<div class="container" >
<div class="row">
<div class=" col-12">
<h1 class= "text-center pt-4">Header content</h1>
</div>
</div>
</div>
</header>
<section class="py-5">
<div class="container" >
<div class="row">
<div class=" col-lg-3">
<div id=" list-example" class="list- group">
<a cla ss="list-group-item list- group-item-action" href="# list-item-1">Item 1</a>
<a cla ss="list-group-item list- group-item-action" href="# list-item-2">Item 2</a>
<a cla ss="list-group-item list- group-item-action" href="# list-item-3">Item 3</a>
<a cla ss="list-group-item list- group-item-action" href="# list-item-4">Item 4</a>
</div>
</div>
<div class=" col-lg-9">
<div data- spy="scroll" data-target="# list-example" data-offset="0" class="scrollspy-example">
<h4 id ="list-item-1">Item 1</h4>
<p>Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id
<ul cl ass="list-group">
< li class="list-group-item">Cra s justo odio</li>
< li class="list-group-item">Dap ibus ac facilisis in</li>
< li class="list-group-item">Mor bi leo risus</li>
< li class="list-group-item">Por ta ac consectetur ac</li>
< li class="list-group-item">Ves tibulum at eros</li>
</ul>
<p>Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id
<h4 id ="list-item-2">Item 2</h4>
<p>Qui s magna Lorem anim amet ipsum do mollit sit cillum voluptate ex nulla tempor. Laborum
</p>
<p>Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id
<h4 id ="list-item-3">Item 3</h4>
<p>Qui s anim sit do amet fugiat dolor velit sit ea ea do reprehenderit culpa duis. Nostrud
<p>Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id
<p>Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id
<h4 id ="list-item-4">Item 4</h4>
<p>Qui s anim sit do amet fugiat dolor velit sit ea ea do reprehenderit culpa duis. Nostrud
</div>
</div>
</div>
</div>
</section>
<footer>
<div class="container" >
<div class="row mt-5">
<div class=" col-12">
<h2 class= "text-center pt-4">Footer content</h2>
</div>
</div>
</div>
</footer>
<script>
var pageHeight = $(doc ument).height(); // Main page height
var footerHeight = $(' footer').height(); // footer container height
var dataContentHeight = $('#list-example').height();
var footerBottomHeight = pageHeight - footerHeight;
var bottomHeight = foo terBottomHeight - dataContentH eight;
var mainpagewidth = $( window).width();
if (mainpagewidth > 99 2) {
// it would be work when we go on the responsive
$(window).scroll(f unction () {
if (jQuery(doc ument).scrollTop() > 260) {
jQuery('# list-example').addClass(' fixedClass').css('top', '0');
if (jQuery (document).scrollTop() > botto mHeight) {
jQuery ('#list-example').removeAttr(' style').css('bottom', footerHe ight);
}
} else {
jQuery('# list-example').removeAttr(' style').removeClass(' fixedClass');
}
});
} else {
jQuery('#list- example').removeAttr('style'). removeClass('fixedClass');
}
</script>
</body>
No comments:
Note: Only a member of this blog may post a comment.