0

我有一个隐藏了溢出的div,并设置了高度,在里面我有一个位置相对和顶部的图像:0px

我想知道如何将计数添加到 css 位置 top: 0px 所以每次用户单击“向上按钮”时,图像在 div 内向上移动 10px。

我似乎无法让 count++ 工作,请在此处查看 JS Fiddle: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');
});
4

2 回答 2

2

尝试:

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

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

这里的工作示例。

于 2013-07-20T11:28:55.860 回答
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/

于 2013-07-20T11:29:20.173 回答