Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use jquery Library to realize Elevator Navigation effect

Shulou Source: shulou.com Published: 2022-06-01 13:05:55 09月28日 Update

This article mainly introduces how to use jquery library to achieve elevator navigation effect, the article is very detailed, has a certain reference value, interested friends must read it!

Basic ideas

Elevator navigation basically uses the height of the element from the page header offsetTop and the page scrolling distance scrollTop to compare the corresponding results.

1. The page scrolls to the corresponding position to realize the display and hiding of elevator navigation.

2. The page scrolls to the appropriate location, and the elevator navigation button adds or removes the corresponding class

3. Click the elevator navigation button to scroll the page and add or remove the corresponding classes for the button.

4. Click the top button to return to the top

Code implementation

Html code

Head focus picture parent-child beauty learning travel accommodation food tail parent-child beauty learning tourism accommodation food top

Css code

* {padding: 0; margin: 0;} body {font-size: 30px;} .header {width: 1100px; height: 200px; background-color: pink; margin: 0 auto;} .banner {width: 1100px; height: 400px Background-color: skyblue; margin: 0 auto;} .footer {width: 1100px; height: 300px; background-color: darkolivegreen; margin: 0 auto;} .content {width: 1100px; margin: 0 auto } .content .qinzi {width: 100%; height: 324px; background-color: rosybrown;} .content .liren {width: 100%; height: 304px; background-color: slategrey;} .content .xuexi {width: 100%; height: 300px Background-color: khaki;} .content .lvyou {width: 100%; height: 300px; background-color: greenyellow;} .content .zhusu {width: 100%; height: 300px; background-color: darkcyan;} .content .meishi {width: 100% Height: 300px; background-color: lightgreen;} .width: 50px; position: fixed; top: 150px; left: 50%; margin-left:-620px; font-size: 16px; text-align: center Li {width: 50px; height: 30px; background-color: grey; margin-bottom: 5px; line-height: 30px; list-style: none; cursor: pointer;} span {display: block; width: 50px; height: 30px Background-color: grey; margin-bottom: 5px; line-height: 30px; list-style: none; cursor: pointer;} .manag.current {background-color: hotpink;}

JavaScript code

Var flag = true; / / use throttle / / the page is just beginning to be hidden. When the page scrolls to content, the elevator navigation shows $(function () {/ / page refresh) and calls the / / encapsulation function once, switching between showing and hiding var contentTop = $(".content"). Offset (). Top; toggleTool () Function toggleTool () {if ($(document). ScrollTop () > = contentTop) {$(".clients") .fadeIn ();} else {$(".clients") .fadeOut () }} $(window) .scroll (function () {toggleTool () / / page to the appropriate location Add current class if (flag) {$('.content .w') .each (function (I, ele) {var cuHeight = ele.offsetHeight / 2) to the elevator navigation button If ($(document) .scrollTop () > = $(ele) .offset () .top-cuHeight) {$('.floor li') .eq (I) .addClass (' current') .siblings () .removeClass () }})}) / / Click the elevator navigation button, the page jumps to the appropriate location, and use animate $('.floor li') .click (function () {flag = false) in jquery $(this) .addClass ('current'). Siblings (). RemoveClass (); var index = $(this) .index (); var current = $(' .content .w') .eq (index) .offset () .top $('html,body') .stop () .animate ({scrollTop: current}, function () {flag = true;})}) $(' .floor span') .click (function () {$(this) .addClass ('current')) $('html,body'). Stop (). Animate ({scrollTop: 0})}) above are all the contents of the article "how to use the jquery Library to achieve Elevator Navigation effects". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Pages elevators navigation buttons code location effects top beauty parent-child content head articles food learning travel events values elements interests Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Microsoft Docker Shulou Information