jsBin demo
$(function() {
var $imgs = $('#imgs'), // Cache your elements
$img = $('img', $imgs), // Cache your elements
lbDown = false,
Y = 0, // Get mouse Y coord
exY = 0; // Used to store old Y value and compare with
// new one to get the direction.
$imgs.on("mousedown mouseup",function(e) {
if (e.which === 1) {
e.preventDefault();
lbDown = e.type=="mousedown"; // Boolean true/false
}
}).on("mousemove", function(e) {
if(lbDown) {
e.preventDefault();
Y = e.clientY; // Get new value
var $visible = $(this).find('img:visible'); // Get the visible one
$img.not(":first").hide(); // Hide all (but first)
$visible[Y>exY ? "next" : "prev"]().show(); // Ternary Operator
exY = Y; // Remember old value
}
});
// You might also want to add mouseleave and set lbDown to false.
});
您需要的代碼修正也隱藏重疊圖像才能看到前一個(鼠標左移)。
稍微不同的方法也將是通過計算
(mousemoveCoordY - clickedInitiallyAtY) % numberOfImages
DEMO
%
(提醒操作員)來獲得.EQ()圖像將創建一個無限循環,只是一個鼠標移動速度更快可能會跳過一些幀*。
$(function() {
var $imgs = $('#imgs'),
$img = $('img', $imgs),
n = $img.length,
lbDown = false,
Y = 0,
clickedY = 0;
$imgs.on("mousedown mouseup",function(e) {
if (e.which === 1) {
clickedY = e.clientY;
e.preventDefault();
lbDown = e.type=="mousedown";
}
}).on("mousemove", function(e) {
if(lbDown) {
e.preventDefault();
$img.hide().eq((e.clientY-clickedY)%n).show();
}
});
// You might also want to add mouseleave and set lbDown to false.
});
*數學(我是說),使其運行變慢會是這樣的:
$img.hide().eq((((e.clientY-clickedY)*0.3)|0)%n).show();
LIKE IN THIS DEMO其中(0.1 - 0.9)是靈敏度(倍增) 你需要。
這是真棒..除了它只滾動水平鼠標的移動方向,當它應該在Y軸,而不是X軸滾動。大大讚賞 – bobafart
@bobafart哈哈哈是的我知道我正在修復我的答案。 –
這是非常驚人的。我將它更改爲JS Fiddle上的Y方向:http:// jsfiddle。網/ C4Y6H/1/ – bobafart