2013-07-20 18 views
0

我有一个div溢出隐藏着一套高度和内我有一个相对位置和顶级的图像:0像素jQuery的点击背景POS Y +数+

我想知道我怎么可以添加一个计数css position top:0px所以每次用户点击“向上按钮”时,图片在div内移动了10px。

我似乎无法得到计数++工作,请参阅JS小提琴这里:http://jsfiddle.net/michelm/wE2Tz/

//var count = 10++; 

$('#button_up').click(function(){ 
    $('#banner img').css('top', '-10px') ; 
}); 

$('#button_down').click(function(){ 
    $('#banner img').css('top', '10px'); 
}); 
+0

你有没有试着用你的逻辑结合的点击? – Jaykishan

回答

2

尝试用:

$('#button_up').click(function(){ 
    $('#banner img').css('top', '-=10px') ; 
}); 

$('#button_down').click(function(){ 
    $('#banner img').css('top', '+=10px'); 
}); 

工作实例here

+0

谢谢乔纳森。它工作得很好:) 我也试过,但我的语法错了。非常感谢 – Michel

0

试试这个:

$('#button_up').click(function(){ 
    var currentTop = $('#banner img').position().top; 
    $('#banner img').css('top', currentTop - 10) ; 
}); 

$('#button_down').click(function(){ 
    var currentTop = $('#banner img').position().top; 
    $('#banner img').css('top', currentTop + 10) ; 
}); 

http://jsfiddle.net/3wEMz/