Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 1.63 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. jQuery add css value incrementally
  2. $('.up').click(function(){
  3.    $('.list').css('top','-20px');
  4. });
  5.        
  6. <div id="container">
  7.     <ul class="list">
  8.         <li >first item</li>
  9.         <li class="active">second item</li>
  10.         <li>third item</li>
  11.         <li >fourth item</li>
  12.     <ul>
  13. </div>  
  14. <a href="#" class="up">up</a>
  15. <a href="#" class="up">down</a>​
  16.        
  17. $('.up').click(function(){
  18.    $('.list').css({position:'absolute', top: parseInt($('.list').css('top'), 10) + 20 + 'px'});
  19. });
  20.        
  21. $('.up').click(function(){
  22.    $('.list').css({position:'absolute'});
  23.    $('.list').animate({top: '+=20'});
  24. });
  25.        
  26. $('.up').click(function(){
  27.    $('.list').css({position:'absolute', top:'-20px'});
  28. });
  29.        
  30. $('.up').click(function(){
  31.     var newtop = $('.list').position().top - 20;
  32.     $('.list').css('top', newtop + 'px');
  33. });
  34.  
  35. $('.down').click(function(){
  36.     var newtop = $('.list').position().top + 20;
  37.     $('.list').css('top', newtop + 'px');
  38. });
  39.        
  40. <a href="#" id="up">up</a>
  41. <a href="#" id="down">down</a>​
  42.        
  43. $('#up').on('click', function(){
  44.   currentValue = parseInt($('#container').css("margin-top"));
  45.   currentValue -= amount;
  46.   $('#container').css("margin-top", currentValue)
  47. })
  48.  
  49. $('#down').on('click', function(){
  50.   currentValue = parseInt($('#container').css("margin-top"));
  51.   currentValue += amount;
  52.   $('#container').css("margin-top", currentValue)
  53. })
  54.        
  55. getElementById("nameofyourelement").style['top'] = "5px"
  56.        
  57. getElementById("nameofyourelement").style['-webkit-transition-duration'] = "1s" //1 second duration for animation, set to 0 if you don't want animation
  58. getElementById("nameofyourelement").style['-webkit-transform'] = "translateY(5px)"