简体   繁体   English

动画 .scrollTop

[英]Animating .scrollTop

When I click, I trigger .scrollTop and it seems to work.当我点击时,我触发.scrollTop并且它似乎工作。 But page goes up/down directly.但是页面直接向上/向下。 How to animate this?如何动画这个? So user can really understand whats happening.所以用户可以真正了解发生了什么。 I need to animate this piece of code: jQuery(window).scrollTop(jQuery(".tabs").offset().top);我需要为这段代码设置动画: jQuery(window).scrollTop(jQuery(".tabs").offset().top);

Whole Java Script:整个Java脚本:

jQuery(document).ready(function() {

jQuery('.tabs .tab-links a').on('click', function(e)  {
    var currentAttrValue = jQuery(this).attr('href');

    // Show/Hide Tabs
   jQuery('.tabs ' + currentAttrValue).slideDown(400).siblings().slideUp(400); 

   console.log(currentAttrValue);
    jQuery(window).scrollTop(jQuery(".tabs").offset().top);
    jQuery(".tab-links li").removeClass("active");
    jQuery('a[href="'+currentAttrValue+'"]').parent('li').addClass('active');

   e.preventDefault();
});

}); });

HTML: HTML:

<div class="tabs">
    <ul class="tab-links">
        <li class="active"><a href="#tab1">Tab #1</a></li>
        <li><a href="#tab2">Tab #2</a></li>
    </ul>  

    <div class="tab-content">

        <div id="tab1" class="tab active">
                 <div id="lipsum">Long content</div>
        </div>

        <div id="tab2" class="tab">
                 <div id="lipsum">Long content</div>
        </div> 

    </div>

    <div class="tabs">
    <ul class="tab-links">
        <li class="active"><a href="#tab1">Tab #1</a></li>
        <li><a href="#tab2">Tab #2</a></li>
    </ul>
</div>
</div>

jsFiddle: js小提琴:

http://jsfiddle.net/4m2ut16k/12/ http://jsfiddle.net/4m2ut16k/12/

use jquery animate method for animation effect.使用 jquery animate 方法制作动画效果。

jQuery('.tabs ' + currentAttrValue).slideDown(1000,function() {
       jQuery(this).animate({opacity: '0.5'});
}).siblings().slideUp(1000,function(){
    jQuery(this).animate({opacity: '0.5'});   
});

Live link http://jsfiddle.net/4m2ut16k/12/直播链接http://jsfiddle.net/4m2ut16k/12/

You need to use animate() like,你需要使用animate() 之类的,

jQuery('html,body').animate({ // html,body not window
                 'scrollTop':jQuery(".tabs").offset().top
           },1000); //1000 is duration here

Live Demo现场演示

Update if the tabs already visible then animate only like,如果选项卡已经可见,则更新,然后只设置动画,例如,

// Show/Hide Tabs, if visible then animate
if (jQuery('.tabs ' + currentAttrValue).is(':visible')) {
  jQuery('html,body').animate({
       'scrollTop': 0  // scroll to 0 to make it consistent for both tabs
  }, 1000);
} else {
   jQuery('.tabs ' + currentAttrValue).slideDown(400)
                                      .siblings().slideUp(400);
}

Updated Fiddle更新的小提琴

Another updated, you need to check the height of siblings with the current one and then check for scroll top position like,另一个更新,您需要检查当前兄弟姐妹的高度,然后检查滚动顶部位置,例如,

var $curr=jQuery('.tabs ' + currentAttrValue),
    $sib=jQuery('.tabs ' + currentAttrValue).siblings();
// Show/Hide Tabs, slidedown then animate       

if($curr.height() < $sib.height()){
    if(jQuery('html,body').scrollTop() == 0){
        $curr.slideDown(400).siblings().slideUp(400);
    } else {
        jQuery('html,body').animate({
            'scrollTop': 0
        }, 1000);
        $curr.show().siblings().hide();
    }     

} else {        
    $curr.slideDown(400,function(){
        jQuery('html,body').animate({
            'scrollTop': 0
        }, 1000);
    }).siblings().slideUp(400);
}

Another Updated Fiddle另一个更新的小提琴

你可以使用 JQuery 的animate方法也检查这个例子**jsFiddle:** http://jsfiddle.net/gxkuS/176/

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

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