我试图制作这些图像,当您单击它们时,它们会滚动到data-down属性中给定的ID。但是,当我尝试它不会scoll。当您在控制台日志中找到包含要滚动到的id的变量时,它是正确的,因此我不知道为什么它不起作用。

<header>
    <div id="header-content">
        <h1>Welcome to the JETS Lake Garda Campaign</h1>
        <img class="down" data-down="#about" src="images/down.png">
    </div>
</header>
<section id="about">
    <h2>About Us</h2>
    <p>Libero minus dicta, sapiente, dolor, quidem quisquam magnam expedita eos voluptates saepe itaque maiores facilis sit nihil consequuntur vero hic possimus inventore autem. Voluptate amet dicta corporis dolorum facilis, quisquam.</p>
    <img class="down" data-down="#sailors" src="images/down.png">
</section>
<section id="sailors">
    <h2>Sailors</h2>
    <p>Libero minus dicta, sapiente, dolor, quidem quisquam magnam expedita eos voluptates saepe itaque maiores facilis sit nihil consequuntur vero hic possimus inventore autem. Voluptate amet dicta corporis dolorum facilis, quisquam.</p>
    <div class="down" data-down="#sponsors" src="images/down.png">
</section>
<section id="sponsors">
    <h2>Sponsors</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi, itaque, pariatur. Aliquam ab in, ullam nostrum quis eos, commodi at? Eos quod amet dignissimos dolorum consequuntur adipisci dolore ducimus quis.</p>
</section>


$(document).ready(function () {
    $('.down').on('click', function () {
        var toGo = $(this).data('down');
        console.log(toGo);
        $('html,body').animate({
            scrollTop: $(toGo).offset.top
        }, 'slow');
    });
});


http://jsfiddle.net/8bc15bdu/

最佳答案

您必须将$(toGo).offset.top替换为$(toGo).offset().top

关于javascript - jQuery根据数据属性滚动到部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28537450/

10-13 01:39