Analysis of Baidu Encyclopedia Directory Navigation Tree Widget
This article mainly introduces the Baidu Encyclopedia Directory Navigation Tree widget, which has certain reference value. Let’s take a look at it together
It’s embarrassing to say that I have been in the garden for 4 years and have 3 registered accounts. It’s been more than a year and I haven’t written a blog. The reasons why I didn’t write a blog before were: 1. I felt that my level was too weak and I didn’t dare to come out to mislead others. I was also afraid of being laughed at by the big guys. 2. I was too lazy. Sometimes I make something small by myself, and I am very interested in it during the process. But after I finish it, I feel it is boring and I am too lazy to spend time to sort it out. I don’t want to continue with this idea in the new year. The change starts today.
Let me first introduce the background of wheel-making: I designed a prototype for a customer a few days ago, which is a page for displaying and scoring data on one step. On this page, customers can see the working steps of the APP configuration and the collected data. The data can be scored separately for each step. When designing, it is considered that generally there are many work steps configured on the APP side. When the web background is displayed, the page will be very long, and the user may be in the process of viewing the data and scoring. I don’t know how many steps I have rated and how many steps are left unscored, so I want to design something similar to navigation on the page. Through this navigation, I can clearly and intuitively see which step I am currently browsing. At the same time, you can also click on the step you are interested in and scroll directly to the content area of that step. At that time, I had a flash of inspiration and thought of the directory navigation tree on the right side of Baidu Encyclopedia. Why not just use this effect? It basically meets the effect I want, so I drew a prototype page according to this effect and confirmed it with the customer. The customer was also quite satisfied. , after the prototype is determined, the task begins. Let’s start with this navigation tree. From the perspective of maintainability and reuse, I wanted to directly encapsulate a plug-in. On the function page, call it directly through JQ, so that the amount of code on the function page will be less, so With this little thing, let’s take a look at the renderings first:
1. Introduction to control parameters
1, data: Provides a data source for control generation. The navigation names such as Title 1, Title 2, and Title 3 in the rendering are obtained through the NodeName of this attribute.
2, css: Provide css style for the navigation tree container. This can be adjusted according to personal needs, such as controlling the distance of the navigation tree from the top and right side of the browser.
3, className: This parameter is mainly used to position the navigation tree cursor to the corresponding node when the browser scroll bar scrolls to the corresponding content. The default value is '.item'.
Currently there are only these three parameters. You can expand the parameters you want according to your needs when using it.
2. Control call
1, js part
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script src="NavigationTree.js"></script> <script> $(function () { //创建控件 var tree = $('#demo').navigationTree({ data: [ { ID: '1', NodeName: '标题1' }, { ID: '2', NodeName: '标题2' }, { ID: '3', NodeName: '标题3', Children: [{ ID: '3.1', NodeName: '标题3.1' }, { ID: '3.2', NodeName: '标题3.2' }] }, { ID: '4', NodeName: '标题4' }, { ID: '5', NodeName: '标题5' } ] }); }); </script>
2, how about the control html part
<!--控件容器开始--> <p id="demo"></p> <!--控件容器结束-->
, is it relatively simple to call?
3. Description of Implementation Difficulties
In fact, the most difficult part of the entire function may be how to accurately display the area where the current user is browsing in the directory navigation tree. This is mainly By listening to the scroll bar scrolling event, and then dynamically calculate which element is currently in the browser's visible area in the event, then get the unique identifier (ID) of the element, and then find the corresponding node in the directory navigation tree based on the ID. , calculate the distance between the node and the top of the parent element, and control the top value of the cursor element. I know that when I finish saying this, you may still not understand, so please take a look at the code. The code is sometimes better than others' verbal explanation. It is much more intuitive and clear:
//#region滚动条事件 var $win = $(window); var winHeight = $win.height(); $win.scroll(function () { var winScrollTop = $win.scrollTop(); for (var i = _allElements.length - 1; i >= 0; i--) { var elmObj = $(_allElements[i]); //!(滚动条离顶部的距离>元素在当前视图的顶部相对偏移+元素外部高度)&&!(滚动条离顶部的距离<元素在当前视图的顶部相对偏移-window对象高度/2) if (!(winScrollTop > elmObj.offset().top + elmObj.outerHeight()) && !(winScrollTop < elmObj.offset().top - winHeight/2)) { $('.arrow').css({ top: $('[data-id="' + elmObj.attr('id') + '"]').position().top + 3 }); return false; } } }); //#endregion
The variable _allElements saves the object array obtained through the className parameter. The array is continuously cycled in the scroll event to compare which element is in the currently visible area. Within, then get the ID of the element, find the corresponding node in the directory tree, get the distance between the node element and its parent element, and give the distance to the $('.arrow') object through css. The $( '.arrow') object is the blue cursor object on the right. By controlling its top value, you can adjust the position where it is displayed to the corresponding node.
4. Additional small functions
Because in my usage scenario, I need to be able to indicate that the step has been scored, so when encapsulating this control, additional This small function has been added, but by default the "Completed" small icon will not be displayed. When called through the following js code, the icon will be displayed behind the corresponding node:
//控制第二个节点显示已完成 tree.showOkIcon(2);
where tree The object is the object returned after creating the control. Through the showOkIcon method of the object, the small icon is displayed. The parameter is the ID of the corresponding node. The rendering is as follows:
That’s all. Content, because it is my first time to write a blog, and my level is limited, so the code implementation may not be elegant and concise enough. Please take a look and pat lightly. I hope it can bring you some help. ,
Attached download link:http://pan.baidu.com/s/1kVFf8I7
The above is the entire content of this article, I hope it will help everyone learn Helpful, please pay attention to the PHP Chinese website for more related content!
Related recommendations:
About Jquery zTree tree control asynchronous loading operation
How to use Js to dynamically create div
SpringBoot and SpringSecurity handle Ajax login request issues
The above is the detailed content of Analysis of Baidu Encyclopedia Directory Navigation Tree Widget. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PyCharm is a powerful and popular Python integrated development environment (IDE) that provides a wealth of functions and tools so that developers can write code more efficiently. The plug-in mechanism of PyCharm is a powerful tool for extending its functions. By installing different plug-ins, various functions and customized features can be added to PyCharm. Therefore, it is crucial for newbies to PyCharm to understand and be proficient in installing plug-ins. This article will give you a detailed introduction to the complete installation of PyCharm plug-in.
![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

How to use WordPress plug-ins to achieve instant location functionality With the popularity of mobile devices, more and more websites are beginning to provide geolocation-based services. In WordPress websites, we can use plug-ins to implement instant positioning functions and provide visitors with services related to their geographical location. 1. Choose the right plug-in. There are many plug-ins that provide geolocation services in the WordPress plug-in library to choose from. Depending on the needs and requirements, choosing the right plug-in is the key to achieving instant positioning functionality. Here are a few

How to Add WeChat Mini Program Functions to WordPress Plugins With the popularity and popularity of WeChat mini programs, more and more websites and applications are beginning to consider integrating them with WeChat mini programs. For websites that use WordPress as their content management system, adding the WeChat applet function can provide users with a more convenient access experience and more functional choices. This article will introduce how to add WeChat mini program functionality to WordPress plug-in. Step 1: Register a WeChat mini program account. First, you need to open the WeChat app

How to use WordPress plug-in to implement video playback function 1. Introduction The application of video on websites and blogs is becoming more and more common. In order to provide a high-quality user experience, we can use WordPress plug-ins to implement video playback functions. This article will introduce how to use WordPress plugins to implement video playback functions and provide code examples. 2. Choose plug-ins WordPress has many video playback plug-ins to choose from. When choosing a plug-in, we need to consider the following aspects: Compatibility: Make sure the plug-in

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to
