【问题标题】:Toggle Background Position using jQuery使用 jQuery 切换背景位置
【发布时间】:2012-02-07 11:00:17
【问题描述】:

我是 jQuery 新手,在尝试让某些东西正常工作时遇到了一些麻烦。

基本上我有一个 wordpress 网站,每个页面上都有一个不同的背景图片,用于 body 标签。我希望能够打开一个按钮,然后将主体背景图像下降约 500 像素。

基本上我的页面顶部有一个隐藏的联系区域,当您单击按钮 (a.contact) 时,隐藏的联系区域 (#contactArea) 会从顶部下拉显示,但是当在您再次单击该按钮之前,contactArea 会隐藏我的一些背景图片。

我想要实现的是,当隐藏的contactArea 被显示时,背景图像会下降(仍然完全可见),以便背景图像始终可见。我希望这是有道理的?!

我的css代码是:

body.page.page-id-240 {
   background:url(images/main-home-bg.jpg) center 600px no-repeat;
}

我当前的 jquery 是:

$(window).load(function() {
    $("#contactArea").css('height', '0px');

    $("a.contact").toggle( 
        function () { 
            $("#contactArea").animate({ height: "225px" }, { queue:false, duration: 500, easing: 'linear' } )
        }, 
        function () { 
            $("#contactArea").animate({ height: "0px" }, { queue:false, duration: 500, easing: 'linear' }) 
        } 
    ); 
});

如果有人可以提供帮助,将不胜感激! :-)

【问题讨论】:

  • 你不能把你的背景图片放在#contactArea上吗?
  • 感谢您的帮助。 - 否,因为背景图像是分配给 body 元素的类 (.page)。 contactArea 部分工作正常,它只是隐藏了一些身体背景图像,我试图让身体背景图像在 contactArea 在切换时下拉。

标签: jquery wordpress background background-image background-position


【解决方案1】:

试试这个:

$("a.contact").toggle( 
    function () { 
        $("#contactArea").animate({ height: "225px" }, { queue:false, duration: 500,  easing: 'linear' } )
        $("body.page.page-id-240").animate({ backgroundPosition: "center 825px" });
    }, 
    function () { 
        $("#contactArea").animate({ height: "0px" }, { queue:false, duration: 500, easing: 'linear' }) 
        $("body.page.page-id-240").animate({ backgroundPosition: "center 600px" });
    } 
); 

【讨论】:

  • 您好 Rory McCrossan 感谢您的帮助,我尝试了您的建议,但是背景图像仍然无法切换。我是否不必在某处包含我已添加到 body 元素的 body 类(.page.page-id-240)?
  • 是的,你是对的。我有一个大脑衰退。我已经更新了答案。
  • 尝试了您的建议,但仍然没有看。当 contactArea 显示时,身体图像仍然没有移动。不知道我做错了什么——让我发疯。你认为有可能有两个身体类,并且在切换时会显示一个不同的类。所以你会有: class1)body.page.page-id-240 { background:url(images/main-home-bg.jpg) center 600px no-repeat; } 然后是 class2) body.page.page-id-240 { background:url(images/main-home-bg.jpg) center 900px no-repeat; }
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
相关资源
最近更新 更多