Home Web Front-end JS Tutorial Detailed examples of how jQuery implements accordion menu, hierarchical menu, top menu, and seamless scrolling effect

Detailed examples of how jQuery implements accordion menu, hierarchical menu, top menu, and seamless scrolling effect

Dec 31, 2017 pm 04:31 PM
jquery accomplish accordion

This article mainly introduces the graphic description of the accordion menu, hierarchical menu, top menu, and seamless scrolling menu based on jQuery. The effect display and code implementation of this article are introduced in very detail. Friends who need it can refer to it. I hope it can Help everyone.

1. The accordion menu renderings and code are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title>手风琴效果制作</title>

 <link rel="stylesheet" href="../css/reset.css">

 <style>

  .con{

   width:908px;

   height:300px;

   margin:50px auto;

   overflow: hidden;

   position:relative;

  }

  .con .list_ul{

  }

  .con .list_ul li{

   width:805px;

   height:300px;

   position:absolute;

   background:#fff;

  }

  .con .list_ul li span{

   width:26px;

   height:296px;

   text-align: center;

   color:#fff;

   padding-top:4px;

   float:left;

   cursor: pointer;

  }

  .con .list_ul li img{

   width:777px;

   height:300px;

   float:right;

  }

  .con .list_ul li:after{

   display: table;

   clear:both;

   zoom:1;

   content: '';

  }

  .con .list_ul li:nth-child(1){

   left:0;

  }

  .con .list_ul li:nth-child(2){

   left:801px;

  }

  .con .list_ul li:nth-child(3){

   left:828px;

  }

  .con .list_ul li:nth-child(4){

   left:855px;

  }

  .con .list_ul li:nth-child(5){

   left:882px;

  }

  .con .list_ul li:nth-child(1) span{

   background: rgba(8, 201, 160, 0.49);

  }

  .con .list_ul li:nth-child(2) span{

   background: rgba(120, 201, 66, 0.97);

  }

  .con .list_ul li:nth-child(3) span{

   background: rgb(77, 114, 201);

  }

  .con .list_ul li:nth-child(4) span{

   background: rgb(255, 179, 18);

  }

  .con .list_ul li:nth-child(5) span{

   background: rgb(201, 5, 166);

  }

 </style>

 <script src="../js/jquery-1.12.4.min.js"></script>

 <script>

  $(function(){

   $(".list_li").click(function(){

    //左边

    $(this).animate({left:26*$(this).index()});

    //获取该li元素前面的兄弟元素,实现点击中间的部分,它前面的兄弟元素也跟着一起向左移动

    $(this).prevAll().each(function(){

     $(this).animate({left:26*$(this).index()});

    });

    //右边:获取该li元素后面的兄弟元素,实现点击中间的部分,它后面的兄弟元素也跟着一起向右移动

    $(this).nextAll().each(function(){

     $(this).animate({left:778+26*$(this).index()});

    });

   })

  })

 </script>

</head>

<body>

 <p class="con">

  <ul class="list_ul">

   <li class="list_li">

    <span>风景图01</span>

    <img src="../images/li01.png" alt="风景图01">

   </li>

   <li class="list_li">

    <span>风景图02</span>

    <img src="../images/li02.png" alt="风景图02">

   </li>

   <li class="list_li">

    <span>风景图03</span>

    <img src="../images/li03.png" alt="风景图03">

   </li>

   <li class="list_li">

    <span>风景图04</span>

    <img src="../images/li04.png" alt="风景图04">

   </li>

   <li class="list_li">

    <span>风景图05</span>

    <img src="../images/li05.png" alt="风景图05">

   </li>

  </ul>

 </p>

</body>

</html>

Copy after login

2. The scroll-down (level) menu renderings and code are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title>用jQuery中的slideToggle制作菜单</title>

 <link rel="stylesheet" href="../css/reset.css">

 <style>

  .menu{

   width:200px;

   margin:10px auto;

  }

  .menu>li{

   background: #8731dd;

   color:#fff;

   text-indent: 16px;

   margin-top:-1px;

   cursor: pointer;

  }

  .menu>li>span{

   padding:10px 0;

   display:block;

   border-bottom: 1px solid #f3f3f3;

  }

  .menuactive,.menu>li>span:hover{

   background:#c7254e;

  }

  .menu > li ul{

   display: none;

  }

  .menu > li ul li{

   text-indent:20px;

   background: #9a9add;

   border:1px solid #f3f3f3;

   margin-top:-1px;

   padding:6px 0;

  }

  .menu >li .active{

   display: block;

 

  }

  .menu > li ul li:hover{

   background:#67C962;

  }

  .menu_li ul{

   margin-bottom:1px;

  }

 </style>

 <script src="../js/jquery-1.12.4.min.js"></script>

 <script>

  $(function () {

   $(".menu_li>span").click(function(){

    $(this).addClass("menuactive").parent().siblings().children("span").removeClass("menuactive");

    $(this).next("ul").slideToggle();

    $(this).parent().siblings().children("ul").slideUp();

   })

  })

 </script>

</head>

<body>

<ul class="menu" id="menu">

 <li class="menu_li">

  <span class="menuactive">水果系列</span>

  <ul class="active">

   <li>苹果</li>

   <li>梨子</li>

   <li>葡萄</li>

   <li>火龙果</li>

  </ul>

 </li>

 <li class="menu_li">

  <span>海鲜系列</span>

  <ul>

   <li>鱼</li>

   <li>大虾</li>

   <li>螃蟹</li>

   <li>海带</li>

  </ul>

 </li>

 <li class="menu_li">

  <span>果蔬系列</span>

  <ul>

   <li>茄子</li>

   <li>黄瓜</li>

   <li>豆角</li>

   <li>西红柿</li>

  </ul>

 </li>

 <li class="menu_li">

  <span>速冻食品</span>

  <ul>

   <li>水饺</li>

   <li>冰淇淋</li>

  </ul>

 </li>

 <li class="menu_li">

  <span>肉质系列</span>

  <ul>

   <li>猪肉</li>

   <li>羊肉</li>

   <li>牛肉</li>

  </ul>

 </li>

</ul>

</body>

</html>

Copy after login

3. Sticky menu (when a menu reaches the top of the page, stop there)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

 <!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title>置顶菜单</title>

 <link rel="stylesheet" href="../css/reset.css" rel="external nofollow" >

 <style>

  .header{

   width:960px;

   height:200px;

   margin:0 auto;

   background: #ccc;

  }

  .header img{

   width:100%;

   height:200px;

  }

  .ul_list{

   width:960px;

   height:50px;

   margin:0 auto;

   text-align: center;

   display: flex;

   justify-content: space-between;/*实现子元素水平方向上平分*/

   align-items: center;/*使子元素垂直方向上居中*/

   background: #67C962;

  }

  .ul_list li{

   width:160px;

   height:50px;

   line-height: 50px;

   border:1px solid #ccc;

   /*使边框合并*/

   margin-right:-1px;

  }

  .ul_list li a{

   color:#fff;

  }

  .ul_list li:hover{

   background: #c7254e;

  }

  .ul_fixed{

   position: fixed;

   top:0;

  }

  .menu_pos{

   width:960px;

   height:50px;

   margin:0 auto;

   line-height: 50px;

   display: none;

  }

  .con p{

   width:958px;

   height:300px;

   line-height: 300px;

   text-align: center;

   margin:-1px auto 0;

   border: 1px solid #ccc;

  }

  .footer{

   height:300px;

  }

  .top{

   width:38px;

   height:38px;

   border-radius: 35px;

   background: #000;

   color:#fff;

   font-size:13px;

   padding:8px;

   text-align: center;

   position: fixed;

   right:100px;

   bottom:20px;

   display: none;

  }

  .top:hover{

   cursor: pointer;

  }

 </style>

 <script src="../js/jquery-1.12.4.min.js"></script>

 <script>

  $(function(){

   var oLeft = ($(document).outerWidth(true)-$(".header").outerWidth())/2;

   var oTop = $(".top");

   $(window).scroll(function(){

    if($(window).scrollTop() >= $(".header").outerHeight()){

     $(".ul_list").addClass("ul_fixed").css({left:oLeft});

     $(".menu_pos").show();

    }else{

     $(".ul_list").removeClass("ul_fixed");

     $(".menu_pos").hide();

    }

    if($(window).scrollTop() >= 150){

     oTop.fadeIn();

     oTop.click(function(){

      //第一种回到顶部的方式

      //$(window).scrollTop(0);

      //第二种回到顶部的方式(常用)

      $("html,body").animate({scrollTop:0});

     })

    }else{

     oTop.fadeOut();

    }

   });

  })

 </script>

</head>

<body>

 <p class="header">

  <img src="../images/li02.png" alt="banner图">

 </p>

 <ul class="ul_list">

  <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a></li>

  <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新闻动态</a></li>

  <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >产品展示</a></li>

  <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >案例分析</a></li>

  <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >关注我们</a></li>

  <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >联系我们</a></li>

 </ul>

 <p class="menu_pos"></p>

 <p class="con">

  <p class="con_header">网站主内容一</p>

  <p class="con_center">网站主内容二</p>

  <p class="con_footer">网站主内容三一</p>

 </p>

 <p class="footer"></p>

 <p class="top">回到顶部</p>

</body>

</html>

Copy after login

4. None The seam scrolling effect diagram and code are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title>无缝滚动</title>

 <link rel="stylesheet" href="../css/reset.css">

 <style>

  .con{

   width:662px;

   margin:50px auto;

  }

  .con button{

   width:100px;

   height:36px;

   line-height: 36px;

   text-align: center;

   border: none;

   margin-left:20px;

  }

  .box{

   width:655px;

   height:135px;

   margin:20px auto;

   border:1px solid #ccc;

   padding-left:10px;

   padding-bottom:10px;

   position: relative;

   overflow: hidden;

  }

  .ul_list{

   display: flex;

   position: absolute;

   left:0;

   top:0;

   padding: 10px;

  }

  .ul_list li{

   width:120px;

   height:120px;

   border:1px solid #ccc;

   margin-left:-1px;

   margin-right:10px;

   /*margin-top:10px;*/

  }

  .ul_list li img{

   width:100%;

   height:100px;

   line-height: 100px;

  }

 </style>

 <script src="../js/jquery-1.12.4.min.js"></script>

 <script>

  $(function(){

   var oUl = $(".ul_list");

   var oLeft = $(".left");

   var oRight = $(".right");

   var left = 0;

   var delay = 2;

   oUl.html(oUl.html()+oUl.html());

   function move(){

    left-=delay;

    if(left<-667){

     left = 0;

    }

    if(left>0){

     left = -667;

    }

    oUl.css({left:left});

   }

   var timer =setInterval(move,30);

   oLeft.click(function(){

    delay =2;

   });

   oRight.click(function(){

    delay = -2;

   });

   oUl.children().each(function(){

    oUl.eq($(this).index()).mouseover(function(){

     clearInterval(timer);

    });

    oUl.eq($(this).index()).mouseout(function(){

     timer= setInterval(function(){

      left-=delay;

      if(left<-667){

       left = 0;

      }

      if(left>0){

       left = -667;

      }

      oUl.css({left:left});

     },30);

    })

   })

  })

 </script>

</head>

<body>

<p class="con">

 <button class="left">向左</button>

 <button class="right">向右</button>

 <p class="box clearfix">

  <ul class="ul_list">

   <li><img src="../images/furit_03.jpg" alt="第一张图片"></li>

   <li><img src="../images/goods_08.jpg" alt="第二张图片"></li>

   <li><img src="../images/furit_02.jpg" alt="第三张图片"></li>

   <li><img src="../images/goods_05.jpg" alt="第四张图片"></li>

   <li><img src="../images/furit_04.jpg" alt="第五张图片"></li>

  </ul>

 </p>

</p>

</body>

</html>

Copy after login

The knowledge points involved in the above menu are as follows:

4. jQuery

1. slideDown() shows downwards

2, slideUp() rolls up

3, slideToggle() in sequence Expand or roll up an element

5. jQuery chain call

The method of jquery object returns this jquery object after execution. All jQuery The methods of the object can be written together:

1

$("#p1").chlidren("ul").slideDown("fast").parent().siblings().chlidren("ul").slideUp("fase")

Copy after login

$("#p1").//The element with id bit p1

.chlidren("ul") //The ul under this element Child element

.slideDown("fast") //The height is from zero to the actual height to display the ul element

.parent() //Jump to the parent element of ul, which is the id For the element of p1

.siblings() //Jump to all sibling elements at the same level of the p1 element

.chlidren("ul") //Find the ul sub-elements in these sibling elements

.slideUp("fase") //Convert from the actual height to zero to hide the ul element

Related recommendations:

jquery implementation of drop-down menu Accordion effect sharing

Example sharing jquery to achieve multi-level menu effect

jQuery menu select all, invert selection, cancel example analysis

The above is the detailed content of Detailed examples of how jQuery implements accordion menu, hierarchical menu, top menu, and seamless scrolling effect. 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

Video Face Swap

Video Face Swap

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

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

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:

Using PHP to implement SaaS: a comprehensive analysis Using PHP to implement SaaS: a comprehensive analysis Mar 07, 2024 pm 10:18 PM

I'm really sorry that I can't provide real-time programming guidance, but I can provide you with a code example to give you a better understanding of how to use PHP to implement SaaS. The following is an article within 1,500 words, titled "Using PHP to implement SaaS: A comprehensive analysis." In today's information age, SaaS (Software as a Service) has become the mainstream way for enterprises and individuals to use software. It provides a more flexible and convenient way to access software. With SaaS, users don’t need to be on-premises

See all articles