Как перейти к элементу при нажатии на ссылку [дублировать]

0

Поэтому у меня есть следующий веб-сайт: http://www.gameplay-universe.uphero.com/

Вы можете увидеть ссылку "Пропустить к контенту". Я хочу, когда он щелкнул страницу, чтобы прокрутить до div#content. Я использую последнюю версию jQuery. Как я могу это достичь?

Теги:

4 ответа

1
Лучший ответ

Хорошо, вот решение, которое я нашел:

$(document).ready(function() {

// Click event for any anchor tag that href starts with #
$('a[href^="#"]').click(function(event) {

    // The id of the section we want to go to.
    var id = $(this).attr("href");

    // An offset to push the content down from the top.
    var offset = 60;

    // Our scroll target : the top position of the
    // section that has the id referenced by our href.
    var target = $(id).offset().top - offset;

    // The magic...smooth scrollin' goodness.
    $('html, body').animate({scrollTop:target}, 500);

    //prevent the page from jumping down to our section.
    event.preventDefault();
});
});

Это сработало для меня.

1
$("li a").click(function(){
    $('html, body').animate({
        scrollTop: $( $.attr(this, 'href') ).offset().top
    }, 1000);
    return false;
}); 

это будет работать для всех ссылок внутри элементов li.

  • 0
    Так как я новичок в jQuery, какие переменные мне нужно отредактировать, чтобы он div#content onclick li.skip a к div#content onclick li.skip a ?
  • 0
    $ ("# skip a"). click (function () {$ ('html, body'). animate ({scrollTop: $ ("# content"). offset (). top}, 1000); вернуть false; });
0

Вот сценарий, который мне нравится использовать, когда я хочу включить "эффект прокрутки" типа JQuery.

http://cferdinandi.github.io/smooth-scroll/

0
$(document).scrollTop($('div#content').offset().top)

Ещё вопросы

Сообщество Overcoder
Наверх
Меню