Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of switching between tab columns by JavaScript

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces JavaScript how to achieve the effect of tab column switching, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Tab column: click on different tags to display different content, and the style of the clicked tag changes (highlight which one is selected)

The idea of realization is:

1, the tab column is divided into two parts, the top is the navigation list, the following is the corresponding content of each part. Write out the tag and content, and write the content in turn according to the tag order. Add a custom attribute---index to the tag. The attribute value starts at 0 and increases by 1.

2. First of all, realize the above effect, switch the style after clicking, change the font color and background color of the clicked font, etc.

① css defines a class eg:current, which defines the changed style. First, write a class name for the first tag, and the other class names are not written.

② js gets all tag element objects,---for loop---traverses to bind each tag---onclick click event

The ③ click event uses an exclusive algorithm---for loop---sets the class name of all tag settings to empty---className ='', and then the tag currently clicked sets the special class name defined above---this.className = 'current'

3. To implement the following, different contents appear according to the label clicked:

① css displays the corresponding content of the first tag, and hides the rest:

. box-tb. Item:nth-child (nasty 2) {display: none;}

② gets all the element objects of the content, gets the index value of the tag, adds another exclusive thought code in the click event---for loop---traverses the element object of the content, sets all the contents to hide---items [I] .style.display = 'none';, and then displays the corresponding content according to the tag clicked---items[ index] .style.display =' block'

Code example: tab bar toggle * {margin: 0; padding: 0;} li {list-style: none;} .box {width: 600px; margin: 100px auto }. Box-th {overflow: hidden; width: 100%; background-color: # 88ace6;} .box-th ul li {float: left; width: 90px; height: 20px; margin-right: 10px; padding: 5px Text-align: center;} .current {color: # fff; background-color: green;}. Box-tb {margin-top: 20px;} .box-tb. Item:nth-child (nasty 2) {display: none Spring, summer, autumn, winter and spring rank first among the four seasons, and a new cycle begins. Spring represents warmth, growth, plant sprouting, wind and sun warmth, birds singing and flowers scent. The temperature, sunshine and rainfall in this season are at the turning point of the year and tend to rise or increase. In spring, the spirit of yin and yang begins to change, and all things sprout and grow with the rise of yang. Spring is the season for the growth of all things and spring ploughing and sowing. The so-called "plan of the year lies in spring". The beginning and end of spring: divided according to the changes of celestial phenomena: traditionally, it takes "the Beginning of Spring" (Douzhi Yin, when the sun reaches the yellow longitude 315 °) as the starting point of spring and ends with "the Beginning of Summer". According to the change of air temperature: in modern times, the "pentad mean temperature" proposed by scholar Zhang Baoli was used to classify it. It begins in spring when the pentad average temperature (the average temperature for 5 consecutive days) rises steadily from less than 10 ℃ to more than 10 ℃, and ends when the pentad average temperature is above 22 ℃ (in summer). Everything is in full bloom in summer, which is the season when crops enter the peak season. The temperature rises, the weather is hot, high winds and torrential rains occur frequently, and all things flourish. Summer is the season with the most storms, often accompanied by strong winds and torrential rains. In summer in the northern hemisphere, the continental heated pressure rises to form a low pressure, and the ocean constant temperature is relatively low to form a high pressure. According to the circulation, the summer wind blows from the southeast. In summer, the days are the longest in the northern hemisphere all year round. The beginning and end of summer: according to the changes of celestial phenomena, it takes "the Beginning of Summer" (Dou finger southeast, the sun reaches the yellow longitude 45 °) as the starting point of summer to the end of "the Beginning of Autumn". According to the change of air temperature, the summer begins when the pentad average temperature rises steadily above 22 ℃ and ends when the pentad average temperature is below 22 ℃. Autumn is the harvest time, which means that all things begin to grow from luxuriant growth to bleak maturity. The Beginning of Autumn and the End of Heat are still very hot in the first two solar terms in autumn. because the heat brought by the sun has not weakened, the so-called "heat is in three volts," which appears between Lesser Heat and the End of Heat. It is the hottest, humid and muggy day of the year. The reason for the high humidity in dog days is that the wind blows southeasterly in dog days, while the Pacific Ocean and Indian Ocean blow in the southeast. The air is humid, and the humidity of the wind causes high humidity in dog days. In late autumn, on the contrary, the northwest wind blows, while the northwest is dry inland. The dry northwest wind causes the climate to be dry after late autumn. The climate of the first two solar terms in autumn is humid and muggy. After White Dew's solar terms, confessions tend to be cool and dry. With the advent of late autumn, the climate changes from hot to cool, and everything grows with the cold, gradually declining, which is the alternating season of hot and cold. The most obvious change in autumn is that the leaves of plants change from luxuriant green to yellowing, and the deciduous crops begin to mature. [1] the beginning-end of autumn: according to the changes of celestial phenomena, it takes "the Beginning of Autumn" (refers to the southwest, the sun reaches the yellow longitude 135 °) as the starting point of autumn to the end of "the Beginning of Winter". According to the change of air temperature, the average pentad temperature decreases steadily from more than 22 ℃ to less than 22 ℃ as the beginning of autumn, and ends when the pentad average temperature drops below 10 ℃. In winter, yin and yang change, all things are received and stored, and plants are stored. After entering the Beginning of Winter, it means that the dry climate with little rain in autumn has gradually passed, turning into the winter climate of overcast, rainy and cold. The height of the sun in the northern hemisphere is small and the daytime is short. As the saying goes, "hot in three volts" and cold in thirty-nine, "the Winter Solstice is" 19 days. "the Winter Solstice was not very cold before. After the Winter Solstice, the strong cold air went south frequently and crossed the Nanling Mountains. The temperature dropped suddenly and the weather was cold. It is after the Winter Solstice that the winter is really cold. The beginning and end of winter: according to the changes of celestial phenomena, it takes "the Beginning of Winter" (Dou refers to the northwest, the sun reaches the yellow longitude 225o) as the starting point of winter, and ends with the next "the Beginning of Spring". According to the change of air temperature, the winter begins when the pentad average temperature falls steadily below 10 ℃ and ends when the pentad average temperature is above 10 ℃. Var list = document.querySelector ('.box-th'). QuerySelectorAll (' li'); var items = document.querySelector ('. Box-tb'). QuerySelectorAll ('.item'); for (var I = 0; I

< list.length; i++) { list[i].onclick = function() { // 导航栏样式切换,排他算法 for (var i = 0; i < list.length; i++) { list[i].className = ''; } this.className = 'current'; // 下面展示内容切换,排他算法 var index = this.getAttribute('data-index'); for (var i = 0; i < items.length; i++) { items[i].style.display = 'none'; } items[index].style.display = 'block'; } } 页面效果:

On how JavaScript to achieve tab column switching effect is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report