Home Web Front-end JS Tutorial How to use encapsulation to achieve floor lighting effect (code example)

How to use encapsulation to achieve floor lighting effect (code example)

Sep 19, 2018 pm 03:16 PM
jquery js

This chapter introduces how to use encapsulation to achieve the floor lighting effect (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Functional requirements:

1. When the mouse scrolls to the page content floor, the side navigation floor appears; otherwise it is hidden.

2. When the mouse scrolls to the corresponding floor, the side navigation floor is highlighted.

3. When you click on the side navigation, the page scrolls to the corresponding floor.

As shown below:

How to use encapsulation to achieve floor lighting effect (code example)

Technical points:

1. Proficient in using jquery and js,

2. Animation; scrollTop(); offset(). top

3. Anchor application

Implementation process:

1. Encapsulate a For the plug-in that scrolls and lights up the floor, call this method in the main page program

How to use encapsulation to achieve floor lighting effect (code example)

2. Next, we go deep into the plug-in to implement the specific functions of the plug-in.

  1. scrollsoy (options) { function }, here we pass a parameter to the plug-in, as shown in the function above, an object {target: "div.affix"} is passed in, that is, we Class for additional navigation on the side. The advantage of this is to improve the reusability of the code. As long as such a function is needed in the future, we only need to modify the value of the target. This is a plug-in.

  2. Define a rolling event in the plug-in to light up the floor switch.

    i), Get the scrolling distance between the window and the top of the scroll bar, var top = $(window).scrollTop();

    ii), Judgment:

    If now If you scroll above the 1st floor (top

    If you scroll below the 3rd floor (top>3F distance), create a side navigation bar The fade-out effect;

    else, now between the 1st floor and the 3rd floor, make the fade-in effect of the additional navigation bar on the side; At this time, light up the currently scrolling floor switch.

    Traverse each floor of the side navigation bar and check which floor's offset of the window scroll offset exceeds that of the page.
    How to get the offset of the page floor? As shown in the figure above, the href attribute of the a tag on the side of the navigation bar is bound to the page floor id. Get its anchor point on the page through the href attribute of the a tag, and then use offset().top to get the offset of the page floor
    Judgment——top>The offset of the page floor indicates that the window has scrolled to a specific floor. Highlight the floor of the additional navigation bar on the side

  3. Define a click event in the plug-in, click the additional navigation bar on the side, and the page scrolls to the corresponding floor

    First, prevent the default behavior of hyperlinks

    According to the href attribute of a, find the offset of its corresponding floor from the top of the page

    Use animation effects to let the page body scroll to the specified high. Note that there are compatibility issues with the highlighted part here$('body').animate({scrollTop: offset}, 500)

Summary:

I believe that everyone will have a better understanding of encapsulation after reading this example. In the future, when implementing functions, we can think about problems from this perspective to improve the reusability of code. However, if you can try it yourself and taste the actual difficulties encountered, you can discover your own problems and continuously improve your abilities.

The above is the detailed content of How to use encapsulation to achieve floor lighting effect (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

See all articles