简体   繁体   English

滚动到部分

[英]Scroll to Section

I have the following code, my question is how would I go about adding to this so that when a user clicks on a link a scroll to is added so that it takes them to the class .instructors section 我有以下代码,我的问题是我将如何添加到该代码中,以便当用户单击链接时添加滚动到,从而将其带到类.instructors部分。

<?php if($hasSections): ?>

                    <div class="instructors-links">
                        <?php foreach ($sections as $section): ?>
                            <a href="#section<?php echo $section['id']; ?>">
                                <div class="instructors-img">
                                    <img src="<?php echo $section['image']['url']; ?>" />
                                    <h2><?php echo $section['title']; ?></h2>
                                </div>
                            </a>

                        <?php endforeach; ?>
                    </div>

                    <div class="instructors">
                        <?php foreach ($sections as $section): ?>

                            <section id="section<?php echo $section['id']; ?>">
                                <h1 class="section-title"><?php echo $section['title']; ?></h1>
                                <div class="section-content">
                                    <?php echo $section['content']; ?>
                                </div>
                            </section>

                        <?php endforeach; ?>
                    </div>



                    <script type="text/javascript">
                        var $sections = jQuery('.instructors section'), //Each section of content
                            $sectionLinks = jQuery('.instructors-links a'); //Each section link

                        //When a section link <a> tag is clicked
                        $sectionLinks.click(function(e) {
                            e.preventDefault(); //prevent the default actions from happening like following the link and scrolling down to the content

                            changeSection(
                                jQuery(this).attr('href').replace('#', '') //Gets the href value of the clicked <a> tag and removes the "#" character
                            ); //Passes the section link to the "changeSection" function
                        });

                        changeSection('section1'); //When the page loads, display the "section1" section
                        function changeSection(sectionID) {
                            $sections.stop(true).hide(); //Stop animating and hide all sections
                            $sections.filter('#' + sectionID).stop(true).fadeIn(); //Display the section with the same ID as the section link that was clicked

                            $sectionLinks.removeClass('selected'); //Remove the selected class from all section link <a> tags
                            $sectionLinks.filter('[href="#' + sectionID + '"]').addClass('selected'); //Add the "selected" class to the section link that was clicked
                        }

                    </script>

                <?php endif; ?>

The idea is that when a user clicks on a link the text changes and the page scrolls down to the text. 这个想法是,当用户单击链接时,文本会更改,页面会向下滚动到文本。

this should animate a scroll to the '.instructors section' 这应该使滚动动画到“ .instructors部分”

 function changeSection(sectionID) {
        $sections.stop(true).hide(); //Stop animating and hide all sections
        $sections.filter('#' + sectionID).stop(true).fadeIn(); //Display the section with the same ID as the section link that was clicked

        $sectionLinks.removeClass('selected'); //Remove the selected class from all section link <a> tags
        $sectionLinks.filter('[href="#' + sectionID + '"]').addClass('selected'); //Add the "selected" class to the section link that was clicked

        $('html, body').animate({
            scrollTop: $sections.offset().top
        }, 2000);

  }

I have created a "helper" function in JS about scroll 我已经在JS中创建了有关滚动的“帮助器”功能

function goToByScroll (id) {
    // Reove "link" from the ID
    id = id.replace("link", "");
    // Scroll
    $('html,body').animate({
        scrollTop: $("#" + id).offset().top
    },
        'slow');
}

you should pass the id and will work. 您应该通过ID即可使用。

goToByScroll('section1');

Should locate the window in this id 应该在此ID中找到窗口

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

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