与手风琴结合使用的自定义 Jquery Slide-to div 脚本不完全正常工作
我之前为我正在工作的网站上的艺术家部分编写了一个简单的手风琴脚本。手风琴效果很好,但是由于每个部分中显示的内容的大小,我决定最好将每个部分滑到屏幕顶部,以帮助避免每个访问者自行手动滚动。
然而,我遇到了一个小问题 - 我编写的使每个部分滑入视图的当前脚本似乎会发生什么取决于要滑动的 div 相对于视口/屏幕的位置,动画似乎无法正常运行...它几乎就像它需要重置自身或其他东西才能理解正在触发一个新的ID??...我不知道...
这是我当前的标记:
HTML
<div id="locate_artist_01"><!-- Unique ID assigned to each artist wrapper -->
<div class="artist_leadimg">
<h3 class="artist_bandname">ARTIST NAME</h3><!-- Band Name -->
<div class="artist_toggle_trigger" id="artist_01" title="Show/Hide"></div><!-- Toggle Button -->
</div><!-- .artist_leadimg -->
<div class="artist_toggle_container"></div>
</div>
<!-- ....repeated for each artist, but with different unique ID's -->
JQUERY - 手风琴
$(document).ready(function(){
//Hide (Collapse) the toggle containers on load
$('.artist_toggle_container').hide();
//On Click
$('.artist_toggle_trigger').click(function(){
if( $(this).parent().next().is(':hidden') ) { //If immediate next container is closed...
$('.artist_toggle_trigger').removeClass('artist_toggle_active').parent().next().slideUp(); //Remove all "active" state and slide up the immediate next container
$(this).toggleClass('artist_toggle_active').parent().next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
}
return false; //Prevent the browser jump to the link anchor
});
});
JQUERY - 幻灯片到艺术家 - 这是我需要帮助的部分
$(document).ready(function(){
$('#artist_01').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_01").offset().top
}, "slow");
});
$('#artist_02').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_02").offset().top
}, "slow");
});
$('#artist_03').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_03").offset().top
}, "slow");
});
$('#artist_04').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_04").offset().top
}, "slow");
});
$('#artist_05').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_05").offset().top
}, "slow");
});
});
我希望有人能指出我正确的方向,因为我感觉非常接近让它正常工作,但我只是不知道足够的 java/jquery 还没有弄清楚我可能缺少什么...
提前感谢您的帮助。
I've previously scripted a simple accordion for an artists section on a site I'm working on. The accordion works great however due to the size of the content thats revealed in each section I decided it would be best if I made each section slide to the top of the screen to help avoid having each visitor scroll manually on their own.
Ive run into a small problem however - what seems to happen with the current script Ive written to make each section slide into view is depending where the div to be slided is in relation to the viewport/screen, the animation does not seem to function correctly...its almost like it needs to reset itself or something in order to understand that a new ID is being triggered??...I dunno...
Here is what my current markup looks like:
HTML
<div id="locate_artist_01"><!-- Unique ID assigned to each artist wrapper -->
<div class="artist_leadimg">
<h3 class="artist_bandname">ARTIST NAME</h3><!-- Band Name -->
<div class="artist_toggle_trigger" id="artist_01" title="Show/Hide"></div><!-- Toggle Button -->
</div><!-- .artist_leadimg -->
<div class="artist_toggle_container"></div>
</div>
<!-- ....repeated for each artist, but with different unique ID's -->
JQUERY - Accordion
$(document).ready(function(){
//Hide (Collapse) the toggle containers on load
$('.artist_toggle_container').hide();
//On Click
$('.artist_toggle_trigger').click(function(){
if( $(this).parent().next().is(':hidden') ) { //If immediate next container is closed...
$('.artist_toggle_trigger').removeClass('artist_toggle_active').parent().next().slideUp(); //Remove all "active" state and slide up the immediate next container
$(this).toggleClass('artist_toggle_active').parent().next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
}
return false; //Prevent the browser jump to the link anchor
});
});
JQUERY - Slide to artist - THIS IS THE PART I NEED ASSISTANCE WITH PLEASE
$(document).ready(function(){
$('#artist_01').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_01").offset().top
}, "slow");
});
$('#artist_02').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_02").offset().top
}, "slow");
});
$('#artist_03').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_03").offset().top
}, "slow");
});
$('#artist_04').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_04").offset().top
}, "slow");
});
$('#artist_05').click(function(){
$('html, body').animate({
scrollTop: $("#locate_artist_05").offset().top
}, "slow");
});
});
I'm hoping someone can point me in the right direction because I feel very close to getting this to work correctly but I just dont know enough java/jquery yet to work out what I may be missing...
Thank you for the help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,有两件事。
$('html, body')
的动画。事实上,它对两者进行动画处理,一个接着另一个,因此页面看起来就像倒带一样。有一篇很好的博客文章介绍了解决该问题的方法 这里,但我已将其包含在下面的代码中。因此,删除所有幻灯片到艺术家代码(无需为每个艺术家重复)并尝试以下操作:
Ok two things right off.
$('html, body')
. In fact it animates both, one then the other so the page looks like it rewinds. There is a nice blog post about a way to work around that here, but I've included it in the code below.So remove all of your slide to artist code (there is no need to repeat it for each artist) and try this: