2013-02-26 59 views
2

我有一个DIV,隐藏溢出,对于DIV整个CSS块看起来是这样的:scrollTop的不内DIV溢出工作:隐藏

.mainContainer .display_box .container .row .col_4 .dPost_box{ 
    position:relative; 
    height:100%; 
    width: 100%; 
    overflow:hidden; 
} 

然后,我要打的内容每当我将鼠标悬停在元素上时,DIV都会滚动。但是,它不会工作。我已经尝试jQuery.scrollTop(),以及更新常规scrollTop,它不会工作。我也尝试使用Smooth ScrollAutoscroll 插件,但他们都不会工作。 我现在的Javascript看起来是这样的:

function autoScroll(div){ 
    setInterval(function() { 
    if (div.scrollTop() < div[0].scrollHeight - div.height()){ 
     div.scrollTop(div.scrollTop() + 10); 
    }}, 1000); 
} 
$(document).on({mouseenter: function(){ 
    autoScroll($(this)); 
}, mouseleave: function(){   
}}, '.dPosts_post'); 

我也试过:

function autoScroll(div){ 
    setInterval(function() { 
    if (div.scrollTop() < div[0].scrollHeight - div.height()){ 
     div.scrollTop[0] +=10; 
    }}, 1000); 
} 

但没有什么是可行的。 最好,我想让autoScroll函数正常工作,但如果有任何插件可以工作,我会非常乐意尝试它们。

任何想法将不胜感激。谢谢!

+0

的[?如何溢出隐藏的div一定目前无形元素中滚动]可能重复(http://stackoverflow.com/questions/11301841 /如何对涡旋内-AN-溢出隐藏-DIV至A-一定-当前不可见-ELE) – 2013-02-26 22:26:36

回答

0

我无法得到scrollTop工作,但我来与一个解决方法。下面的代码,如果有其他人在面对未来类似的问题:

funnction autoScroll(div){ 
    //check whether the content does not fit inside the div 
    if(div[0].scrollHeight>div.height()){ 

     //calculate how much the div needs to be scrolled 
     var distance = div[0].scrollHeight - div.height() + 20; 
     var topCoord = parseInt(div.css('top')); //inital top coordinate of the div 
     var lineHeight = Math.floor(parseInt(div.css('font-size')) * 1.5); 

     //calculate approximately how many lines there are in the distance that needs scrolling 
     var linesCt = distance/lineHeight; 

     //scroll the div at a pace of 2.5s per line 
     div.animate({top: topCoord - distance + 'px'}, 2500 * linesCt); 
    } 
}