使用 jQuery 将悬停时的 div 宽度从 50% 更改为 70%

2023-11-29

我有两个 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>

不知道这个是否适合你:http://jsfiddle.net/yCY9y/3/

DOM:

<div id="wrapper">
    <div id="left" class="content_left">LEFT</div><div id="right" class="content_right">RIGHT</div>
</div>

我使用包装器来确保我们永远不会破坏下一行的右侧。

CSS:

#wrapper {
    width:100%;
    overflow:hidden;
    white-space:nowrap;
}
#left, #right {
    display:inline-block;
    width: 50%;
}
#left {
    background:red;
}
#right {
    background:yellow;
}

我用在#wrapper

white-space:nowrap; // Newer break whitespaces (break to the next line)

and

width:100%;

On #left, #right we use:

display:inline-block;

女巫首先兼容>IE6。 (希望这不是问题)。

JS:

$("#left, #right").each(function() {
    $(this).data("standardWidth", $(this).width());
});

$("#left, #right").hover(function() {
    $(this).animate({
        width: "70%"
    }, 300 );
    $(this).parent().children().not(this).animate({
        width: "30%"
    }, 300 );
}, function() {
    $(this).parent().children().each(function() {
        $(this).animate({
            width: $(this).data("standardWidth")
        }, 300 );
    });
});

首先我绑定相同的mouseover and mouseout双方的事件#right and #left

$(selector).hover(mouseOverHandler, mouseOutHandler);

...

$(this).parent().children().not(this)

我们获取事件被触发的元素并找到它的所有父元素(#wrapper) 子节点:$(this).parent().children()现在我们过滤掉所有匹配的内容this使用 jQuery 的not方法。 (this = #left OR #right)女巫是元素。现在我们有#right -> #left and #left -> #right.

The mouseOutHandler重置所有#wrapperchildNodes 的宽度为 50%

希望这能引导您走上正确的道路......

EDIT:

如果您的动画要链接/排队使用过期,可以使用stop停止所有活动动画并清除队列的方法:

$(selector).stop().animate({
    ....
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 jQuery 将悬停时的 div 宽度从 50% 更改为 70% 的相关文章