1

I'm breaking my head trying to make this work. I want my page's body background to scroll sideways.

<html>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>
$(function(){
    $("body").animate({backgroundPosition : "500 0"}, 2000);
});
</script>

<body style="background-image: url('bg.jpg')">

</body>

</html>
user1091856
  • 3,032
  • 6
  • 31
  • 42

2 Answers2

1

jQuery cannot animate css properties that takes 2 or more values.

You can use backgroundPositionY and backgroundPositionX to animate vertically and horizontally:

$("body").animate({backgroundPositionY : "500px"}, 2000);

DEMO

Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
1

In order to create this effect in all browsers, you can try a different approach like this;

HTML

<body>
  <div class="background" id="background"></div>
  <div class="page-content">Put your content here</div>
</body>

CSS

.background {
  background-image:url('bg.jpg');
  position:absolute;
  z-index:1;
}
.page-content {
  z-index:2;
  position:relative;
}

jQuery

$(document).ready(function(){
  var windowHeight = $(window).height();
  var windowWidth = $(window).width();
  var animateWidth = '500'        
  $('#background').height(windowHeight).width(windowWidth)
    .animate({
      left: animateWidth + 'px',
      width: (windowWidth - animateWidth) + 'px'
    }, 2000);
});

Here is a working example: http://jsfiddle.net/HHgKW/3/

onur
  • 395
  • 1
  • 3
  • 8