Similar to this? http://codepen.io/jkochis/pen/ZBxgKd
Js
$(document).ready(function () { $(document).on("scroll", onScroll); //smoothscroll $('a[href^="#"]').on('click', function (e) { e.preventDefault(); $(document).off("scroll"); $('a').each(function () { $(this).removeClass('active'); }) $(this).addClass('active'); var target = this.hash, menu = target; $target = $(target); $('html, body').stop().animate({ 'scrollTop': $target.offset().top+2 }, 500, 'swing', function () { window.location.hash = target; $(document).on("scroll", onScroll); }); }); }); function onScroll(event){ var scrollPos = $(document).scrollTop(); $('#menu-center a').each(function () { var currLink = $(this); var refElement = $(currLink.attr("href")); if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { $('#menu-center ul li a').removeClass("active"); currLink.addClass("active"); } else{ currLink.removeClass("active"); } console.log($('#menu-center ul li a.active').attr("href"), $('#menu-center a:eq(0)').attr("href")); if($('#menu-center ul li a.active').attr("href") !== $('#menu-center a:eq(0)').attr("href")) { $('body').addClass('fixed-product'); } else { $('body').removeClass('fixed-product'); } }); }
HTML
<div class="m1 menu"> <div id="menu-center"> <ul> <li><a class="active" href="#section1">Section 1</a> </li> <li><a href="#section2">Section 2</a> </li> <li><a href="#section3">Section 3</a> </li> <li><a href="#section4">Section 4</a> </li> </ul> </div> </div> <div id="section1"></div> <div id="section2"> <div class="product">PRODUCT</div> </div> <div id="section3"></div> <div id="section4"></div>
CSS
body, html { margin: 0; padding: 0; height: 100%; width: 100%; } .menu { width: 200px; height: 400px; background-color: rgba(0, 0, 0, 1); position: fixed; background-color:rgba(4, 180, 49, 0.6); -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; transition: all 0.3s ease; } .light-menu { width: 100%; height: 75px; background-color: rgba(255, 255, 255, 1); position: fixed; background-color:rgba(4, 180, 49, 0.6); -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; transition: all 0.3s ease; } #menu-center { width: 980px; height: 75px; margin: 0 auto; } #menu-center ul { margin: 15px 0 0 0; } #menu-center ul li { list-style: none; margin: 0 30px 0 0; display: block; } .active { font-family:'Droid Sans', serif; font-size: 14px; color: #fff; text-decoration: none; line-height: 50px; } a { font-family:'Droid Sans', serif; font-size: 14px; color: black; text-decoration: none; line-height: 50px; } #section1 { background-color: grey; height: 100%; width: 100%; overflow: hidden; } #section2 { height: 100%; width: 100%; } #section3 { background-color: blue; height: 100%; width: 100%; } #section4 { background-color: red; height: 100%; width: 100%; } .product { background: yellow; float: right; padding: 100px } .fixed-product .product { position: fixed; top: 0; right: 0; }
Disclaimer: I found this JSFiddle: https://jsfiddle.net/cse_tushar/Dxtyu/141/ and founded my Codepen.
source share