最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery animate marginRight not working, but marginLeft works just fine - Stack Overflow

programmeradmin2浏览0评论

I want to make a background image scroll to the left by using the animate marginRight property which is activated by clicking a link, but its not working. The weird thing is that when I use marginLeft it works, its just backwards. Here is my test site, and here is my non working jQuery code: (Again im trying to get the background image to scroll to the left) Also please let me know if I can post anything else to make this easier on you.

<script type="text/javascript">
jQuery(document).ready(
function(){
    jQuery('#homelink').click(
        function(){
            jQuery('#SiteBackground').animate({
                marginRight : "1000px"
            },10000);
        });
});
</script>

and my html: (#SiteBackground is what I want to move to the left, and #homelink is the activator)

<img id="SiteBackground" src="/jscottsavage/media/Main/Backdrop.jpg" style="position: absolute; left: 0px; top: 0px; z-index: -1; height: 384px;">

<div id="homelink">Home</div>

I want to make a background image scroll to the left by using the animate marginRight property which is activated by clicking a link, but its not working. The weird thing is that when I use marginLeft it works, its just backwards. Here is my test site, and here is my non working jQuery code: (Again im trying to get the background image to scroll to the left) Also please let me know if I can post anything else to make this easier on you.

<script type="text/javascript">
jQuery(document).ready(
function(){
    jQuery('#homelink').click(
        function(){
            jQuery('#SiteBackground').animate({
                marginRight : "1000px"
            },10000);
        });
});
</script>

and my html: (#SiteBackground is what I want to move to the left, and #homelink is the activator)

<img id="SiteBackground" src="/jscottsavage/media/Main/Backdrop.jpg" style="position: absolute; left: 0px; top: 0px; z-index: -1; height: 384px;">

<div id="homelink">Home</div>
Share Improve this question edited Nov 12, 2020 at 13:22 peterh 1 asked Feb 14, 2013 at 3:42 user1408440user1408440 2175 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I figured out a way to do this. I'm not sure if its the right/proper way but it works in Firefox, Chrome, and IE 7+ so I'm going to use it until I find a better way to do it. Basically all you do is change the 'marginRight' to a 'marginLeft', and add a - sign in front of the number, as seen below.

<script type="text/javascript">
jQuery(document).ready(
     function(){
        jQuery('#homelink').click(
    function(){
        jQuery('#SiteBackground').animate({
            marginLeft : "-1000px"
        },10000);
    });
});
</script>
发布评论

评论列表(0)

  1. 暂无评论