簡體   English   中英

jQuery-如果條件為true或false,如何在單擊后滾動元素ID?

[英]Jquery - How to scroll on element id after click if condition is true or false?

我的Jquery有問題,我只想在元素可見時滾動元素,如果隱藏則不滾動。 你能幫助我嗎 ? 非常感謝你。

jQuery的:

$('#menu').click(function(e){
e.preventDefault();
$('#nav').slideToggle('300');
if($("#nav").css('visibility') == 'hidden') {

}else{
    $('#nav h4 a').trigger('click');
    $('html, body').animate({
    scrollTop: $("#nav").offset().top
  }, 1000);   


}

HTML:

<div id="header" data-role="header" data-position="fixed" data-theme="b" style="border:none;" >
  <div class="ui-btn-right" data-role="controlgroup" data-type="horizontal">
    <a id="menu" href="#"><img width="37px" src="/css/menu.png" alt="menu" /></a> <!-- icon to show element or hide -->
  </div>
</div>


<div id="nav" data-role="collapsible" data-theme="b"> <!-- Code for element where to scroll -->
            <h4>Menu</h4>                                        
            <div data-role="listview" data-inset="true">
            ..content
            </div>
</div>

嘗試這個

$('#menu').click(function(e){
  e.preventDefault();
  $('#nav').slideToggle('300');
  if($('#nav').is(':visible') ) // if true means
  {
    $('#nav h4 a').trigger('click');
    $('html, body').animate({
    scrollTop: $("#nav").offset().top
    }, 1000); 
  }
});
console.log($('#nav').is(':visible') )//it will return true or false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM