Home > Web Front-end > JS Tutorial > Analysis of Baidu Encyclopedia Directory Navigation Tree Widget

Analysis of Baidu Encyclopedia Directory Navigation Tree Widget

不言
Release: 2018-06-25 12:01:04
Original
1938 people have browsed it

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>
Copy after login

2, how about the control html part

<!--控件容器开始-->
<p id="demo"></p>
<!--控件容器结束-->
Copy after login

, 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
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template