2013-02-26 63 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); 
    } 
}