使用 jQuery 将悬停时的 div 宽度从 50% 更改为 70%
我有两个 div,每个宽度为 50%。我想让悬停的 div 扩大到 70%,另一个缩小到 30%。当鼠标移出时,它们都会恢复到 50%。我写了一个脚本,但它没有给出正确的结果。宽度是可变的,因此它们需要调整以适应所有窗口尺寸。我怎样才能让这项工作正常进行?
我还没有编写 mouseout 函数,因为 mouseover 无法正常工作。
现在的运作方式如下: http://jsfiddle.net/kYZyp/
这是我的代码:
<div id="left" class="content_left"></div>
<div id="right" class="content_right"></div>
这是我的 div 的 css
.content_left {
width:50%;
top:0px;
left:0px;
bottom:0px;
background:url(wedding.jpg) left center no-repeat;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";
filter: alpha(opacity=90);
-moz-opacity:0.9;
-khtml-opacity: 0.9;
opacity: 0.9;
}
.content_right {
width:50%;
top:0px;
right:0px;
bottom:0px;
background:url(events.jpg) right center no-repeat;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";
filter: alpha(opacity=90);
-moz-opacity:0.9;
-khtml-opacity: 0.9;
opacity: 0.9;
}
我是使用这个脚本:
<script>
$("#left").mouseover(function(){
$("#left").animate({
width: "70%",
opacity: 1
}, 1500 );
$("#right").animate({
width: "30%"
}, 1500 );
});
$("#right").mouseover(function(){
$("#right").animate({
width: "70%",
opacity: 1
}, 1500 );
$("#left").animate({
width: "30%"
}, 1500 );
});
</script>
并包括这个 jQuery 文件:
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
I have two divs that have 50% width each. I want to make it so that the the hovered div expands to 70% and the other reduces to 30%. And when the mouse moves out, they both return to 50% each. I wrote a script but it doesn't give the right results. The widths are fluid so they need to adjust to all window sizes. how Can I make this work right?
I didn't write the mouseout function yet as the mouseover doesn't function correctly.
Here's how it works now:
http://jsfiddle.net/kYZyp/
Here's my code:
<div id="left" class="content_left"></div>
<div id="right" class="content_right"></div>
Here's my css for the div's
.content_left {
width:50%;
top:0px;
left:0px;
bottom:0px;
background:url(wedding.jpg) left center no-repeat;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";
filter: alpha(opacity=90);
-moz-opacity:0.9;
-khtml-opacity: 0.9;
opacity: 0.9;
}
.content_right {
width:50%;
top:0px;
right:0px;
bottom:0px;
background:url(events.jpg) right center no-repeat;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";
filter: alpha(opacity=90);
-moz-opacity:0.9;
-khtml-opacity: 0.9;
opacity: 0.9;
}
And i'm using this script:
<script>
$("#left").mouseover(function(){
$("#left").animate({
width: "70%",
opacity: 1
}, 1500 );
$("#right").animate({
width: "30%"
}, 1500 );
});
$("#right").mouseover(function(){
$("#right").animate({
width: "70%",
opacity: 1
}, 1500 );
$("#left").animate({
width: "30%"
}, 1500 );
});
</script>
And including this jQuery file:
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道这是否适合您: http://jsfiddle.net/yCY9y/3/
DOM :
我使用包装器来确保我们永远不会破坏下一行的右侧。
CSS:
我在
#wrapper
上使用,
在
#left, #right
上我们使用:witch 首先兼容 >IE6。 (希望这不是问题)。
JS:
首先我在
#right
和#left
上绑定相同的mouseover
和mouseout
事件...
我们获取事件被触发的元素 throw 并找到它的所有父节点 (
#wrapper
) childNodes:$(this).parent().children()
现在我们过滤掉所有内容匹配this
使用jQuery 的not
方法。 (this
=#left
OR#right
) 女巫是元素。现在我们有了#right -> #left
和#left -> #正确
。mouseOutHandler
将所有#wrapper
childNodes 的宽度重置为 50%希望这会引导您找到正确的方法...
编辑:
如果您的动画链接/排队使用可以使用
stop
方法停止所有活动动画并清除队列:Doesn't know if this suites you: http://jsfiddle.net/yCY9y/3/
DOM:
I use the wrapper to be sure we never break the RIGHT to the next line.
CSS:
I use on
#wrapper
and
On
#left, #right
we use:witch is first compatible with >IE6. (hopes this is not a problem).
JS:
First i Bind the same
mouseover
andmouseout
event on both#right
and#left
...
We take the element the event is fired throw and finds all it's parents (
#wrapper
) childNodes:$(this).parent().children()
Now we filter out everything matchingthis
using jQuery'snot
method. (this
=#left
OR#right
) witch is the element. Now we have#right -> #left
and#left -> #right
.The
mouseOutHandler
resets all of#wrapper
childNodes's width to 50%Hopes this leads you the right way...
EDIT:
If you are expiring your animation to chain / queue up use can use the
stop
method witch stop all active animations and clears the queue:这应该很适合您:
编辑:添加动画
编辑:为动画添加了高度调整
编辑:如果您希望它填充窗口的高度,只需使用
window.innerHeight
代替容器高度:这是一个 jsFiddle 演示了它的工作原理。
This should work nicely for you:
EDIT: Added animation
EDIT: Added height resize to animation
EDIT: If you want it to fill the height of the window, just use
window.innerHeight
in place of the container height:Here's a jsFiddle that demonstrates it working.
采取@James ' 回答 (+1) 并添加动画,只需使用
.animate()
:演示:http:// jsfiddle.net/mattball/sAW2c/
To take @James' answer (+1) and add animation, just use
.animate()
:Demo: http://jsfiddle.net/mattball/sAW2c/