search
HomeWeb Front-endJS TutorialAnalysis of Baidu Encyclopedia Directory Navigation Tree Widget

Analysis of Baidu Encyclopedia Directory Navigation Tree Widget

Jun 25, 2018 pm 12:01 PM
plug-inBaidu Encyclopedia

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 = $(&#39;#demo&#39;).navigationTree({
  data: [
   { ID: &#39;1&#39;, NodeName: &#39;标题1&#39; },
   { ID: &#39;2&#39;, NodeName: &#39;标题2&#39; },
   {
   ID: &#39;3&#39;,
   NodeName: &#39;标题3&#39;,
   Children: [{ ID: &#39;3.1&#39;, NodeName: &#39;标题3.1&#39; }, { ID: &#39;3.2&#39;, NodeName: &#39;标题3.2&#39; }]
   },
   { ID: &#39;4&#39;, NodeName: &#39;标题4&#39; },
   { ID: &#39;5&#39;, NodeName: &#39;标题5&#39; }
  ]
  });
 });
</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)) {
  $(&#39;.arrow&#39;).css({ top: $(&#39;[data-id="&#39; + elmObj.attr(&#39;id&#39;) + &#39;"]&#39;).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!

Statement
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
Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.