简体   繁体   English

导航栏下拉列表自动打开和自动关闭?

[英]Navbar dropdown list auto open and auto close?

I added a dropdown list to the navbar on another site and added the class .open to the list. 我在另一个站点的导航栏中添加了一个下拉列表,并在列表中添加了.open类。 My intention is as follows: upon load the webpage navbar list contains an img element and opens displaying a promotional offer. 我的意图如下:加载后,网页导航栏列表包含img元素并打开以显示促销信息。 So far so good, the page loads and the list drops displaying the ad, and if clicked it then closes. 到目前为止,页面已加载,列表随即显示广告,如果单击,则关闭。

Ok what I am aiming for is adding a function via jquery or JavaScript or css which will automatically CLOSE the dropdown list after about 5 seconds. 好的,我的目标是通过jquery或JavaScript或CSS添加一个函数,该函数会在大约5秒钟后自动关闭下拉列表。 I have read that the .open class in bootstraps.min.css is not cleared by default and therefore will remain open unless it is 'clicked' to close it. 我已经了解到bootstraps.min.css中的.open类默认情况下不会清除,因此除非“单击”关闭它,否则它将保持打开状态。

 <div class="navbar-responsive">
   <ul class="nav navbar-nav">
   <li class="active">
     <li class="open dropdown-menu">
      <a href="#" Id="test" class="dropdown-toggle" data-               toggle="dropdown"><strong class="caret">

  <ul class="dropdown-menu">
     <li>
         <a href="#">Click to close.</a>
      </li>

           <li>
             <img src="image folder/my_ad_image.png" 
            </li>
           </ul>
        </li>
     </li>
   </ul>
     </div><!---end nav collapse--->
    </div><!---end container--->
   </div>>!---end main navbar--->

This above is what I have written. 以上是我写的。 It rests atop an already existing navbar. 它位于已经存在的导航栏上。 Thanks for reading. 谢谢阅读。

If anyone has any suggestion or could point me in the right direction with respect to tying a jquery timeout function to my .open class or id that would be great. 如果有人有任何建议或可以将我的.open类或id绑定到jquery超时函数的正确方向,那将是很棒的。 So far I have been unable to tie a jquery function or css to my dropdown list Thanks. 到目前为止,我还无法将jQuery函数或CSS绑定到我的下拉列表中,谢谢。

You can use setTimeout() to implement timers in javascript. 您可以使用setTimeout()在javascript中实现计时器。

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. setTimeout()方法将在指定的毫秒数后调用一个函数或对一个表达式求值。

Adapting your code it can be implemented like this: 修改您的代码可以像这样实现:

CSS: CSS:

...
<li id="myid" class="open dropdown-menu">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown"><strong class="caret"></strong></a>
    <ul class="dropdown-menu">
        <li>
            <a href="#">Click to close.</a>
        </li>

        <li> ... </li>
    </ul>
</li>
...

jScript (assuming you're using jQuery): jScript(假设您使用的是jQuery):

$(function() {
    setTimeout(function() {
        $("#myid").removeClass("open")
    }, 5000);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM