2013-10-01 69 views
1

我有一個帶溢出滾動的DIV。並且我想添加2個按鈕來滾動那個div向上和向下。我只能找到一個方法來滾動鼠標。或滾動按鈕,因爲他們希望我設置隱藏溢出。使用按鈕和鼠標滾動DIV

有沒有辦法做到這一點?

+0

請註明滾動方向。垂直或水平。是的,你可以使用jquery插件來實現這一點。你應該嘗試使用谷歌搜索。 –

+0

它必須垂直滾動。我已經嘗試過一些jQuery插件。但他們沒有做我想做的事。 – Daan

回答

2

試試這個:

var step = 25; 
var scrolling = false; 

// Wire up events for the 'scrollUp' link: 
$("#scrollUp").bind("click", function (event) { 
    event.preventDefault(); 
    // Animates the scrollTop property by the specified 
    // step. 
    $("#content").animate({ 
     scrollTop: "-=" + step + "px" 
    }); 
}).bind("mouseover", function (event) { 
    scrolling = true; 
    scrollContent("up"); 
}).bind("mouseout", function (event) { 
    scrolling = false; 
}); 


$("#scrollDown").bind("click", function (event) { 
    event.preventDefault(); 
    $("#content").animate({ 
     scrollTop: "+=" + step + "px" 
    }); 
}).bind("mouseover", function (event) { 
    scrolling = true; 
    scrollContent("down"); 
}).bind("mouseout", function (event) { 
    scrolling = false; 
}); 

function scrollContent(direction) { 
    var amount = (direction === "up" ? "-=1px" : "+=1px"); 
    $("#content").animate({ 
     scrollTop: amount 
    }, 1, function() { 
     if (scrolling) { 
      scrollContent(direction); 
     } 
    }); 
} 

Working fiddle

+0

這工作,謝謝! – Daan

+0

非常感謝,它也適用於我 – Nader