简体   繁体   English

jQuery Scroll To 如何使用

[英]jQuery Scroll To How to use

How can I use jQuery我如何使用jQuery

to this:对此:

<ul class="nav navbar-nav navbar-right">
    <li class="active"><a href="#home">Home <span class="sr-only">(current)</span></a>
    </li>
    <li><a href="#grid" class="scrollto">Grid System</a>
    </li>
    <li><a href="#tooltips" class="scrollto">Tooltips</a>
    </li>
    <li><a href="#tables" class="scrollto">Tables</a>
    </li>
    <li><a href="#carousel" class="scrollto">Carousel</a>
    </li>
    <li><a href="#thumbnails" class="scrollto">Thumbnails</a>
    </li>
    <li><a href="#forms" class="scrollto">Forms</a>
    </li>
</ul>

Here's the JSFIDDLE这是JSFIDDLE

How can it also highlight the active div selected?它还如何突出显示所选的活动 div? (ul li a) (ul li a)

The suggestion would be, first to give id's to the links:建议是,首先给链接提供 id:

<a href="#tables" id="link1">Tables</a>

and then use:然后使用:

$('#link1').scrollTo(document.getElementById('tables'), 800);

as the documentation mentions.正如文档所提到的。

Link to documentation and demos:链接到文档和演示:

http://demos.flesler.com/jquery/scrollTo/ http://demos.flesler.com/jquery/scrollTo/

In this particular case you want to scroll to the href on the links, in jQuery you could do this simply with your current setup, without scrollTo which seems redundant to me.在这种特殊情况下,您想滚动到链接上的 href,在 jQuery 中,您只需使用当前设置即可完成此操作,而无需 scrollTo,这对我来说似乎是多余的。

$(document).ready(function(){ //ready
    $(".scrollTo").click(function(e){
        e.preventDefault();//don't execute click event (Actually going to anchor)
        var element = $(this);
        var elementOffsetTop = $(element.attr("href").offset().top;
        $(body).scrollTop(offset);
    })
});

Alternatively, if you want an animation, replace或者,如果您想要动画,请替换

$(body).scrollTop(offset); 

with

$(body).animate({scrollTop: offset}, 500);

This, would of course make all your ".scrolltop" links work straight away, without libraries besides jQuery.这当然会让你所有的“.scrolltop”链接立即工作,除了 jQuery 之外没有库。

Jquery animate(): Jquery 动画():

You can achieve this using the jquery animate().您可以使用 jquery animate() 来实现这一点。 Use the scrollTop and set the property likewise:使用scrollTop并同样设置属性:

$("html, body").animate({ scrollTop: $(document).height() }, 1000);

If you are looking to scroll to a specific target, then modify the scrollTop property likewise: ( You will have to add an ID or class to your nav ul )如果您要滚动到特定目标,请同样修改scrollTop属性:(您必须将 ID 或类添加到您的 nav ul

$("html, body").animate({ scrollTop: $("#targetID").scrollTop() }, 1000);

jQuery animate docs jQuery 动画文档

Demo演示

Hope this is helpful.希望这是有帮助的。

If you are using Bootstrap, it comes with a plugin ScrollSpy .如果您使用 Bootstrap,它带有一个插件ScrollSpy

Or try this code.或者试试这个代码。

$('#collapse-here .nav li a').on('click', function(e){
    e.preventDefault();
    var thisUrl = $(this).attr('href');
    $("html, body").animate({ scrollTop: $(thisUrl).offset().top }, 1000);
    $(this).parents('li').addClass('active');
    $(this).parents('li').siblings('li').removeClass('active');
});

See Updated CODE - https://jsfiddle.net/rxn04stn/12/请参阅更新的代码 - https://jsfiddle.net/rxn04stn/12/

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

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