Javascript 滚动到第节
我有下面的代码,我的问题是如何添加到这个链接中,这样当用户单击链接时,就会添加一个滚动条,这样就可以将他们带到class.instructors部分Javascript 滚动到第节,javascript,scroll,Javascript,Scroll,我有下面的代码,我的问题是如何添加到这个链接中,这样当用户单击链接时,就会添加一个滚动条,这样就可以将他们带到class.instructors部分 <?php if($hasSections): ?> <div class="instructors-links"> <?php foreach ($sections as $section): ?>
<?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; ?>
其思想是,当用户单击链接时,文本会发生变化,页面会向下滚动到文本。这应该会使滚动到“.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);
}
我在JS中创建了一个关于滚动的帮助函数
function goToByScroll (id) {
// Reove "link" from the ID
id = id.replace("link", "");
// Scroll
$('html,body').animate({
scrollTop: $("#" + id).offset().top
},
'slow');
}
你应该通过身份证才能工作
goToByScroll('section1');
应该在此id中找到窗口您有小提琴或我们可以看到的示例吗?很难理解你是如何组织你的html的。我已经编辑了上面的链接,它会向下滚动到进入页面时的文本,但我应该解释的是,当点击链接时,讲师部分中的文本会发生变化,因此每次点击链接时,页面都会向下滚动。哦,我明白了!我们创建了一个bug,之所以会发生,是因为我们在页面加载时调用了这个函数,将我添加到您的新代码从changeSection函数中删除,并将其放入您的单击处理程序-$sectionLinks.ClickFunction-这应该可以做到,祝您好运!嗨-我不知道这一切都到哪里去了?