[英]how to give left and right scroll buttons for tabs which are dynamically generated
i am generated tabs as per my requirement but when the tabs are becoming more they are coming in next line.AS they look like odd i want to use right and left scroll option. 我根据我的要求生成了选项卡,但是当选项卡越来越多时,它们将进入下一行。它们看起来很奇怪,我想使用向右和向左滚动选项。 The next and previous functionality should work. 下一个和上一个功能应该起作用。 but i do not no how to include this functionality as i am new to script can anyone help me.i am have generated tabs if the tabs are generated more they are coming in next line. 但是我也不是没有办法包含此功能,因为我是脚本的新手,任何人都可以帮助我。如果生成的标签更多,则它们已经生成了标签。 In this functionality all are static tabs how to make them proper to come when they become more. 在此功能中,所有都是静态选项卡,如何使它们在变得更多时适当出现。
function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; return false; }
body {font-family: Arial;} /* Style the tab */ .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } /* Style the buttons inside the tab */ .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; font-size: 17px; } /* Change background color of buttons on hover */ .tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ .tab button.active { background-color: #ccc; } /* Style the tab content */ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }
<p>Click on the buttons inside the tabbed menu:</p> <div class="tab"> <button class="tablinks"onclick="return openCity(event,'London')">Jan 2017</button> <button class="tablinks" onclick="return openCity(event, 'Paris')">Feb 2017</button> <button class="tablinks" onclick="return openCity(event, 'Tokyo')">Mar 2017</button> <button class="tablinks"onclick="return openCity(event,'London')">April 2017</button> <button class="tablinks"onclick="return openCity(event,'London')">May 2017</button> <button class="tablinks"onclick="return openCity(event,'London')">June 2017</button> <button class="tablinks" onclick="return openCity(event, 'Paris')">July 2017</button> <button class="tablinks" onclick="return openCity(event, 'Tokyo')">August 2017</button> <button class="tablinks"onclick="return openCity(event,'London')">September 2017</button> <button class="tablinks"onclick="openCity(event,'London')">October 2017</button> <button class="tablinks"onclick="openCity(event,'London')">Novenber 2017</button> <button class="tablinks" onclick="openCity(event, 'Paris')">December 2017</button> <button class="tablinks" onclick="openCity(event, 'Tokyo')">Jan 2018</button> <button class="tablinks"onclick="openCity(event,'London')">Feb 2018</button> </div> <div id="London" class="tabcontent"> <h3>Januaray</h3> <p>London is the capital city of England.</p> </div> <div id="Paris" class="tabcontent"> <h3>Febrary</h3> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"> <h3>March</h3> <p>Tokyo is the capital of Japan.</p> </div>
what if you just use css to solve this problem. 如果仅使用CSS来解决此问题该怎么办。
add white-space: nowrap;
添加white-space: nowrap;
and overflow-x: scroll;
和overflow-x: scroll;
to .tab
: 到.tab
:
then remove float: left;
然后移除float: left;
from .tab button
and add display: inline-block;
从.tab button
并添加display: inline-block;
instead 代替
this will make your .tab
element scrolling and prevent your buttons from wrapping to the next line. 这将使您的.tab
元素滚动,并防止您的按钮换行到下一行。
/* Style the tab */
.tab {
/* overflow: hidden; */
overflow-x: scroll;
white-space: nowrap;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
/* float: left; */
display: inline-block;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
this will be the result: 结果将是:
function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; return false; }
body {font-family: Arial;} /* Style the tab */ .tab { overflow-x: scroll; white-space: nowrap; border: 1px solid #ccc; background-color: #f1f1f1; } /* Style the buttons inside the tab */ .tab button { background-color: inherit; display: inline-block; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; font-size: 17px; } /* Change background color of buttons on hover */ .tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ .tab button.active { background-color: #ccc; } /* Style the tab content */ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }
<p>Click on the buttons inside the tabbed menu:</p> <div class="tab"> <button class="tablinks"onclick="return openCity(event,'London')">Jan 2017</button> <button class="tablinks" onclick="return openCity(event, 'Paris')">Feb 2017</button> <button class="tablinks" onclick="return openCity(event, 'Tokyo')">Mar 2017</button> <button class="tablinks"onclick="return openCity(event,'London')">April 2017</button> <button class="tablinks"onclick="return openCity(event,'London')">May 2017</button> <button class="tablinks"onclick="return openCity(event,'London')">June 2017</button> <button class="tablinks" onclick="return openCity(event, 'Paris')">July 2017</button> <button class="tablinks" onclick="return openCity(event, 'Tokyo')">August 2017</button> <button class="tablinks"onclick="return openCity(event,'London')">September 2017</button> <button class="tablinks"onclick="openCity(event,'London')">October 2017</button> <button class="tablinks"onclick="openCity(event,'London')">Novenber 2017</button> <button class="tablinks" onclick="openCity(event, 'Paris')">December 2017</button> <button class="tablinks" onclick="openCity(event, 'Tokyo')">Jan 2018</button> <button class="tablinks"onclick="openCity(event,'London')">Feb 2018</button> </div> <div id="London" class="tabcontent"> <h3>Januaray</h3> <p>London is the capital city of England.</p> </div> <div id="Paris" class="tabcontent"> <h3>Febrary</h3> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"> <h3>March</h3> <p>Tokyo is the capital of Japan.</p> </div>
Combining Joel Stüdle's answer with a bit of JS should do the trick: 结合JoelStüdle的答案和一些JS应该可以解决问题:
white-space: nowrap;
添加white-space: nowrap;
and overflow-x: hidden;
和overflow-x: hidden;
to .tab and a bit of margin to the sides (for controls) 到.tab并在边上留一点空白(用于控件) float: left;
删除float: left;
from .tab button and add display: inline-block;
从.tab按钮并添加display: inline-block;
background-color
from .tab to it so that all controls have the same background color 在.tab周围添加一个包装器以将所有导航元素保留在一个空间(称为.tab-navigation )中,将background-color
从.tab移至其中,以便所有控件具有相同的背景色 moveNavigation(byX)
在包装器内,添加两个浮动导航控件.arrow-left和.arrow-right ,它们调用新函数moveNavigation(byX)
moveNavigation(byX)
to do the scrolling 添加一个新函数moveNavigation(byX)
进行滚动 I also created a Fiddle of it so you can play around with it. 我还创建了一个小提琴 ,以便您可以使用它。
function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; return false; } function moveNavigation(byX) { var navigation= document.getElementsByClassName("tab")[0]; navigation.scrollLeft= navigation.scrollLeft + byX; }
/* Style the tab */ .tab-navigation { border: 1px solid #ccc; background-color: #f1f1f1; } .tab { overflow-x: hidden; white-space: nowrap; margin-left: 30px; margin-right: 30px; } /* Style the buttons inside the tab */ .tab button, .tab-control { background-color: inherit; display: inline-block; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; font-size: 17px; } /* Change background color of buttons on hover */ .tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ .tab button.active { background-color: #ccc; } /* Style the tab content */ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } .arrow-left { float: left; } .arrow-right { float: right; }
<p>Click on the buttons inside the tabbed menu:</p> <div class="tab-navigation"> <button class="tab-control arrow-left" onclick="moveNavigation(-100)"> < </button> <button class="tab-control arrow-right" onclick="moveNavigation(100)"> > </button> <div class="tab"> <button class="tablinks" onclick="return openCity(event,'London')">Jan 2017</button> <button class="tablinks" onclick="return openCity(event, 'Paris')">Feb 2017</button> <button class="tablinks" onclick="return openCity(event, 'Tokyo')">Mar 2017</button> <button class="tablinks" onclick="return openCity(event,'London')">April 2017</button> <button class="tablinks" onclick="return openCity(event,'London')">May 2017</button> <button class="tablinks" onclick="return openCity(event,'London')">June 2017</button> <button class="tablinks" onclick="return openCity(event, 'Paris')">July 2017</button> <button class="tablinks" onclick="return openCity(event, 'Tokyo')">August 2017</button> <button class="tablinks" onclick="return openCity(event,'London')">September 2017</button> <button class="tablinks" onclick="openCity(event,'London')">October 2017</button> <button class="tablinks" onclick="openCity(event,'London')">Novenber 2017</button> <button class="tablinks" onclick="openCity(event, 'Paris')">December 2017</button> <button class="tablinks" onclick="openCity(event, 'Tokyo')">Jan 2018</button> <button class="tablinks" onclick="openCity(event,'London')">Feb 2018</button> </div> </div> <div id="London" class="tabcontent"> <h3>Januaray</h3> <p>London is the capital city of England.</p> </div> <div id="Paris" class="tabcontent"> <h3>Febrary</h3> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"> <h3>March</h3> <p>Tokyo is the capital of Japan.</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.