jQuery 背景图像在单击时移动
我是 jQuery 的新手,在尝试让某些东西正常工作时遇到了一些麻烦。
基本上我有一个 WordPress 网站,每个页面上的 body 标签都有不同的背景图像。我希望能够点击一个按钮,然后主体背景图像下降约 500px。
我的CSS代码是:
body.page.page-id-240 {background:url(images/main-home-bg.jpg) center 600px no-repeat;
更新
我的页面顶部有一个隐藏的联系区域,当您单击按钮(a.contact)时,隐藏的联系区域(#contactArea)会显示出来,但是我的一些背景图像被隐藏了直到您再次单击该按钮。
我想要实现的是,当隐藏的联系表单显示时,背景图像会下降(仍然完全可见)。
我当前的 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'})
}
);
});
I am new to jQuery and I am having a bit of trouble with trying to get something working.
Basically I have a WordPress site, on each page is a different background image for the body tag. I want to be able to click on a button and then the body background image to drop about 500px.
My CSS code is:
body.page.page-id-240 {background:url(images/main-home-bg.jpg) center 600px no-repeat;
Update
I have a hidden contact area on the top of my page, and when you click on the button(a.contact) the hidden contact area (#contactArea) is revealed, however some of my background image is hidden until you click on the button again.
What I am trying to achieve is that the background image drops (still completely visible) when the hidden contact form is revealed.
My current jQuery is:
$(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'})
}
);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是在事件处理程序中绑定到一个元素并更改另一个元素的 CSS 的示例:
请注意,
.on()
是 jQuery 1.7 中的新功能,与.bind()< /code> 在旧版本中。
如果您只想更改背景图像的位置,则可以定位
background-position
属性而不是background
属性:这是一个演示:http://jsfiddle.net/DkUwM/1/
更新
在这里,这将重新定位单击
a.contact
元素时,将background-image
附加到body
元素。如果您想要以动画方式更改
background-position
属性,您将需要包含一些更新 jQuery 的代码才能执行此操作:http://www.protofunc.com/scripts/jquery/backgroundPosition/这是一个动画示例
background-position
属性:这是一个演示:http://jsfiddle.net/ DkUwM/4/
请注意,您可以使用
background-position-x
和background-position-y
但 Firefox 不支持这些(它们不在W3C 规范但都是由 Internet Explorer 实现的。Here is an example of binding to an element and changing another element's CSS in the event handler:
Note that
.on()
is new in jQuery 1.7 and is the same as.bind()
in older versions.If you want to only change the position of the background image then you can target the
background-position
property instead of thebackground
property:Here is a demo: http://jsfiddle.net/DkUwM/1/
Update
Here ya go, this will re-position the
background-image
attached to thebody
element when thea.contact
element is clicked.If you want to animate the change of the
background-position
property you will need to include some code that updates jQuery to be able to do so: http://www.protofunc.com/scripts/jquery/backgroundPosition/Here is an example of animating the
background-position
property:Here is a demo: http://jsfiddle.net/DkUwM/4/
Note that you can use
background-position-x
andbackground-position-y
but those are not supported in Firefox (they are not in the W3C spec but are implemented by Internet Explorer.您能否更具体地介绍一下drop。(通过制作500px的drop,上面的背景将是白色的)。我假设您的图像尺寸大于背景所需的尺寸,因此即使在下降后,图像的某些部分也会位于顶部。
首先在 CSS 中:
然后在 jquery 中,
可以使用 div 而不是 body 通过 .animate 函数完成相同的操作。
Could you be more specific about drop.(By making a 500px drop, the above background would be white). I am assuming your image size is more than the background needed so even after dropping down, there will be some part of image on top.
Firstly in CSS:
then in jquery
same could be done with .animate function using a div instead of body.