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

发布于 2024-12-13 14:19:59 字数 1612 浏览 4 评论 0原文

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぺ禁宫浮华殁 2024-12-20 14:20:00

不知道这是否适合您: 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)

width:100%;

#left, #right 上我们使用:

display:inline-block;

witch 首先兼容 >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 );
    });
});

首先我在#right#left上绑定相同的mouseovermouseout事件

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

...

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

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

mouseOutHandler 将所有 #wrapper childNodes 的宽度重置为 50%

希望这会引导您找到正确的方法...

编辑:

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

$(selector).stop().animate({
    ....
})

Doesn't know if this suites you: 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>

I use the wrapper to be sure we never break the RIGHT to the next line.

CSS:

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

I use on #wrapper

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

and

width:100%;

On #left, #right we use:

display:inline-block;

witch is first compatible with >IE6. (hopes this is not a problem).

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 );
    });
});

First i Bind the same mouseover and mouseout event on both #right and #left

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

...

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

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 matching this using jQuery's not 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:

$(selector).stop().animate({
    ....
})
挥剑断情 2024-12-20 14:20:00

这应该很适合您:

<script type="text/javascript">
    $(function(){
        $("#div1").hover(
            function(){
               $(this).css("width", "70%"); 
            },
            function(){
                $(this).css("width", "50%");
            }
        );                             
    });
</script>

编辑:添加动画
编辑:为动画添加了高度调整

<script type="text/javascript">
    $(function(){
        $("#div1").hover(
            function(){
                $(this).animate({ "width" : "70%", "height" : $("#container").height() + "px" }); 
            },
            function(){
                $(this).animate({ "width" : "50%", "height" : "" });
            }
        );                             
    });
</script>
<div id="container" style="height:400px;border:1px solid #000;padding:10px;">
    <div id="div1" style="width:50%;border:1px solid #000;min-height:100px;">
        Hello world!
    </div>
</div>

编辑:如果您希望它填充窗口的高度,只需使用 window.innerHeight 代替容器高度:

<script type="text/javascript">
    $(function(){
        $("#div1").hover(
            function(){
                $(this).animate({ "width" : "70%", "height" : window.innerHeight + "px" }); 
            },
            function(){
                $(this).animate({ "width" : "50%", "height" : "" });
            }
        );                             
    });
</script>
<div id="div1" style="width:50%;border:1px solid #000;min-height:100px;">
    Hello world!
</div>

这是一个 jsFiddle 演示了它的工作原理。

This should work nicely for you:

<script type="text/javascript">
    $(function(){
        $("#div1").hover(
            function(){
               $(this).css("width", "70%"); 
            },
            function(){
                $(this).css("width", "50%");
            }
        );                             
    });
</script>

EDIT: Added animation
EDIT: Added height resize to animation

<script type="text/javascript">
    $(function(){
        $("#div1").hover(
            function(){
                $(this).animate({ "width" : "70%", "height" : $("#container").height() + "px" }); 
            },
            function(){
                $(this).animate({ "width" : "50%", "height" : "" });
            }
        );                             
    });
</script>
<div id="container" style="height:400px;border:1px solid #000;padding:10px;">
    <div id="div1" style="width:50%;border:1px solid #000;min-height:100px;">
        Hello world!
    </div>
</div>

EDIT: If you want it to fill the height of the window, just use window.innerHeight in place of the container height:

<script type="text/javascript">
    $(function(){
        $("#div1").hover(
            function(){
                $(this).animate({ "width" : "70%", "height" : window.innerHeight + "px" }); 
            },
            function(){
                $(this).animate({ "width" : "50%", "height" : "" });
            }
        );                             
    });
</script>
<div id="div1" style="width:50%;border:1px solid #000;min-height:100px;">
    Hello world!
</div>

Here's a jsFiddle that demonstrates it working.

深府石板幽径 2024-12-20 14:20:00

采取@James ' 回答 (+1) 并添加动画,只需使用 .animate()

$(function(){
    $("#div1").hover(
        function(){
           $(this).animate({width: '70%'});
        },
        function(){
            $(this).animate({width: '50%'});
        }
    );                            
});

演示:http:// jsfiddle.net/mattball/sAW2c/

To take @James' answer (+1) and add animation, just use .animate():

$(function(){
    $("#div1").hover(
        function(){
           $(this).animate({width: '70%'});
        },
        function(){
            $(this).animate({width: '50%'});
        }
    );                            
});

Demo: http://jsfiddle.net/mattball/sAW2c/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文