javascript - css background image move - Stack Overflow

I have image in website's header as background. Now, when page is loaded, I'd like to move it

I have image in website's header as background. Now, when page is loaded, I'd like to move it slowly from left to right (about 100 pixels) and then stop. Is there any not too plex way to do that?

I have image in website's header as background. Now, when page is loaded, I'd like to move it slowly from left to right (about 100 pixels) and then stop. Is there any not too plex way to do that?

Share Improve this question asked Jun 16, 2010 at 16:41 zuupszuups 1,1501 gold badge11 silver badges18 bronze badges 1
  • 2 All I can say is that this operation will be damn slow on a whole lot of today's browsers. Your users will get frustrated whenever they open your website. Perhaps you can achieve the effect you want with an animated gif. That may get you faster. – ypnos Commented Jun 16, 2010 at 16:45
Add a ment  | 

4 Answers 4

Reset to default 2

jQuery will allow you to do that easily.

$("#theImage").animate({
    left: "+=100px", 
}, "slow");

You should check to make sure it only animates on the first page load, not from internal site links. I like to use jquery for this sort of thing.

// animate on first load only
if ( document.referrer == null || document.referrer.indexOf(window.location.hostname) < 0 ) {
$("#logo").animate({ 
    marginLeft: "100px",
    easing: 'swing'
}, 3000 );  // adjust your duration here (milliseconds)
} else { 
    // internal site link set the proper position
    $("#logo").css({ marginLeft: "100px"});
}

Thanks, Rap and ghoppe! That was a helpful hint. I actually wanted to move the background image, not its container so I first set its position in css:

.top-back-image { background-position: -100px 0px; }

and then with jQuery:

$(".top-back-image").animate({ 
  backgroundPosition: "0px 0px",
  easing: 'swing'
}, 3000 );

The guys over at LaunchList have a moving background. Looking through their CSS, this is what I found. Maybe it will be of some help.

#clouds {
    width: 100%;
    height: 650px;
    top: 200px;
    display: block;
    position: fixed;
    background: url(../art/cloud.png) 500px 0 repeat-x;
    -webkit-animation-name: move;
    -webkit-animation-duration: 400s;
    -webkit-animation-iteration-count: infinite;
    z-index: 2;
}

Note that this will only show for webkit browsers.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744644708a4585545.html

相关推荐

  • javascript - css background image move - Stack Overflow

    I have image in website's header as background. Now, when page is loaded, I'd like to move it

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信