【问题标题】:how to make page scroll position to top when we clicked jquery vertical tab menu当我们单击jquery垂直选项卡菜单时如何使页面滚动位置到顶部
【发布时间】:2013-12-04 00:35:20
【问题描述】:

我的演示链接:http://www.bajistech.info/tiltindicators.html#TiltWatch-Plus1,当我单击垂直选项卡时,我正在尝试使页面滚动。

//脚本1 $(document).ready(function(){

    if (
        $('ul#verticalNav li a').length &&
        $('div.section').length
    ) {
        $('div.section').css( 'display', 'none' );
        //$('ul#verticalNav li a').each(function() {
        $('ul#verticalNav li a').click(function() { 
            showSection( $(this).attr('href') );
        });
  // if hash found then load the tab from Hash id
        if(window.location.hash) 
        {
   // to get the div id
           showSection( window.location.hash);
        }
        else // if no hash found then default first tab is opened
        {
            $('ul#verticalNav li:first-child a').click();
        }
    }
});
</script>

//脚本2

function showSection( sectionID ) {
    $('div.section').css( 'display', 'none' );
    $('div'+sectionID).css( 'display', 'block' );
}
$(document).ready(function(){

    if (
        $('ul#verticalNav li a').length &&
        $('div.section').length
    ) {
        $('div.section').css( 'display', 'none' );
        //$('ul#verticalNav li a').each(function() { // no need for each loop
        $('ul#verticalNav li a').click(function() { // Use $('ul#verticalNav li a').click
            showSection( $(this).attr('href') );
        });
        //});
        if(window.location.hash) // if hash found then load the tab from Hash id
        {
           showSection( window.location.hash);// to get the div id
        }
        else // if no hash found then default first tab is opened
        {
            $('ul#verticalNav li:first-child a').click();
        }
    }
});

html源代码

 <ul>
    <li><a href="#a">a</a></li>
    <li><a href="#b">b</a></li>          
    </ul>
    <div id="sections">
    <div class="section" id="a">
    </div>
    <div class="section" id="b">
    </div>

【问题讨论】:

  • 在您的演示中,滚动似乎工作得很好。您是否尝试实现动画滚动效果?
  • 嗨,我想显示页眉和页面顶部。谢谢

标签: javascript jquery html css


【解决方案1】:

添加

$('body').scrollTop(0);

到点击函数。

【讨论】:

    【解决方案2】:

    @dollarvar 的回答并不完美。

    $('html').scrollTop(0); -- for IE only
    $('body').scrollTop(0); -- for all browsers except IE
    $('body,html').scrollTop(0); -- for all browser
    

    我做了一个快速演示。代码如下:

    HTML:

        <div style="height:400px; line-height:400px; background-color: gray; text-align:center; color:white; margin:20px;">Empty space for test</div>
        <ul id="verticalNav">
            <li><a href="#a">a</a></li>
            <li><a href="#b">b</a></li>
        </ul>
        <div id="sections">
            <div class="section" id="a">Content A. Content A. Content A. Content A. Content A. </div>
            <div class="section" id="b" style="display: none;">Content B. Content B. Content B. Content B. Content B. </div>
        </div>
        <div  style="height:800px; line-height:800px; background-color: gray; text-align:center; color:white; margin:20px;">Empty space for test</div>
    

    jQuery:

    <script type="text/javascript">
    $(document).ready(function(){
        $("#verticalNav a").click(function(){
            // show or hide content
            $($(this).attr("href")).show().siblings().hide();
    
            // scroll to the position of #verticalNav
            $('body,html').animate({
                scrollTop: $("#verticalNav").offset().top
            }, "slow");
    
            return false;
        });
    });
    </script>
    

    【讨论】:

    • @user2913707 我以为你想滚动到垂直导航。你可以滚动到任何你想要的地方。 scrollTop: $("#header").offset().top scrollTop: 1000
    • 您好,谢谢,当我单击垂直选项卡时,我正在尝试显示页眉,我需要使用#di bajistech.info/tiltindicators.html#TiltWatch-Plus1 显示以下链接。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多