简体   繁体   English

jQuery,WordPress-单击按钮ID以显示边栏,但也滚动到它

[英]jQuery, WordPress - Click button id to show sidebar but also scroll to it

Firstly, thank you in advance for supporting my question. 首先,首先感谢您支持我的问题。

I believe the outcome will be straight-forward; 我相信结果将是直截了当的。 but I have a WordPress button with id " sidebar-toggle " and its represented as an icon with css. 但我有一个ID为“ sidebar-toggle ”的WordPress按钮,它用css表示为图标。 When this button is clicked, a sidebar appears, and when clicked again (to close) the sidebar hides. 单击此按钮时,将显示一个侧边栏,再次单击(关闭)侧边栏将隐藏。

However, this is my index.php page which shows latest posts. 但是,这是我的index.php页面,其中显示最新帖子。 So when the sidebar appears, it appears all the way down the page, underneath the latest posts. 因此,当出现侧边栏时,它会一直显示在页面的最下方,位于最新帖子的下方。

How would you best ultilise jQuery to successfully scroll down to the sidebar div when the button is clicked? 当单击按钮时,如何最好地利用jQuery成功向下滚动到侧栏div?

CSS CSS

<button id="sidebar-toggle" class="sidebar-toggle"></button>

HTML HTML

<div id="sidebar" class="sidebar"> 
<div id="sidebar-inner" class="sidebar-inner">
// all inner content e.g. text is here
</div>
</div>

jQuery jQuery的

jQuery(document).ready(function() {
jQuery("#sidebar-toggle").live("click", function(event){
jQuery('html,body').animate({ scrollTop:$('#sidebar').offset().top});
// the sidebar doesn't appear until clicked - problem when scrolling?
});
});

EDITED JQUERY 编辑的查询

        jQuery(function() {
   jQuery("#sidebar-toggle").on( "click", function() { //Click

        if (jQuery("#sidebar").is(":visible")) { //Check to see if element is visible then scroll to it
            jQuery('html,body').animate({ //animate the scroll
                scrollTop: $('#sidebar').offset().top 
            }, "slow")
        }
    });
  return false; //This works similarly to event.preventDefault(); as it stops the default link behavior
});
});

I have created a simle example of how it should work. 我已经创建了一个有关如何工作的简单示例。 Hope it will help you :) 希望它能对您有所帮助:)

 $(function() { $( "#sidebar-toggle" ).on( "click", function() { //Click $( "#sidebar" ).slideToggle( "slow", function(){ if ($(this).is(":visible")) { //Check to see if element is visible then scroll to it $('html,body').animate({ //animate the scroll scrollTop: $(this).offset().top }, "slow") } }); return false; //This works similarly to event.preventDefault(); as it stops the default link behavior }); }); 
 .sidebar{ margin-top:500px; height:500px; width:200px; background:red;display:none} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="sidebar-toggle" class="sidebar-toggle">toggler</button> <div id="sidebar" class="sidebar"> <div id="sidebar-inner" class="sidebar-inner"> // all inner content eg text is here </div> </div> 

To check only if statement: 仅检查if语句:

   jQuery(function() {
    if (jQuery('#sidebar').is(":visible")) { //Check to see if element is visible then scroll to it
                    jQuery('html,body').animate({ //animate the scroll
                        scrollTop: jQuery('#sidebar').offset().top 
                    }, "slow")
                }
)};

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

相关问题 jQuery:单击按钮以显示具有ID的覆盖 - Jquery: button click to show an overlay with an id jQuery如果在元素隐藏之外单击,则隐藏,但如果在按钮上单击则显示/隐藏 - JQuery if click outside the element hide but also show/hide if cliked on a button jQuery按钮点击滚动 - jquery button scroll on click 显示从悬停到按钮单击的侧边栏 - show up sidebar from hover to button click 根据按钮单击在jQuery中的显示div动态添加按钮ID - Dynamically add Button id based on the button click show div in jquery 当用户单击MENU按钮时滚动到具有ID的元素(如果子页面也重定向到另一页面) - Scroll to element with ID when user click on MENU button ( also redirect to another page if its subpage) 单击显示时的jQuery显示mouseleave隐藏侧边栏 - Jquery on click show mouseleave hide sidebar jQuery侧边栏-显示子菜单div并在单击按钮时隐藏具有相同类的所有其他菜单 - jQuery Sidebar - Show submenu div and hide all others with same class on button click jQuery滚动到按钮单击部分 - jQuery scroll to sections in button click 单击侧边栏时如何隐藏我的表格? 并且当我单击按钮显示另一个表时,当前打开的表会关闭吗? - How can I hide my table when I click in the sidebar? and Also when I click the button to show another table the current open table will close?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM