简体   繁体   English

将某个div的宽度增加10%,同时将鼠标悬停在另一个div上

[英]Increase a width of a certain div by 10% while hovering on another div

I am trying to achieve the following: 我正在努力实现以下目标:

while hovering on a certain div (class="up"), i would like another div (class="bar") increase its width by 10%, and while hovering another div (class="down"), decrease its width (class="bar") by 10%. 当悬停在某个div(class =“ up”)上时,我希望另一个div(class =“ bar”)将其宽度增加10%,同时将鼠标悬停在另一个div(class =“ down”)上,减小其宽度( class =“ bar”)10%。

I have this so far: 到目前为止,我有:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js"></script>

<script type="text/javascript">//<![CDATA[ 
$(function(){
 $(".bar").css({
     'backgroundColor': 'red'
 }).hover(function () {
      $(this).stop().animate({
               width: '+=10%'
     }, 350).addClass('shadow');
 }, function () {
     $(this).stop().animate({
         width: '200px'
     }, 350).removeClass('shadow');
 });
});//]]>  

</script>

But as i am quite new to JS, i cant figure out how to amend it to suit my needs. 但是由于我对JS还是很陌生,所以我想不出如何根据自己的需要进行修改。 I need it to trigger once i hover a different div then the actual resized div, also i need to use the actual divs with as the .stop().animate width. 一旦我将鼠标悬停在另一个div上,然后再调整实际大小的div上,我就需要触发它。我还需要使用实际的div作为.stop()。animate宽度。 and i need to have both increase in width (while hovering on div with class "up", and decrease while hovering class "down") 并且我需要同时增加宽度(将鼠标悬停在类“上”时在div上,而将鼠标悬停在“下”类上时在减小)

will appreciate your assistance on the matter. 非常感谢您对此事的协助。

thanks in advance. 提前致谢。

the hover listener should be attached to up and down classes not bar 悬停侦听器应附加到上下班而不是酒吧

var bar = $('.bar'),
    originalWidth = bar.width();

$('.up').hover(function(){
  changeBarWidth( 1.1 * originalWidth,true);
},function(){
  changeBarWidth(originalWidth,false);
});

$('.down').hover(function(){
  changeBarWidth( .9 * originalWidth ,false);
},function(){
  changeBarWidth(originalWidth,false);
});

function changeBarWidth(x,addShadow){
  bar.stop().animate({
    width: x
  }).toggleClass('shadow',addShadow);
}

http://jsbin.com/mupomilo/1 http://jsbin.com/mupomilo/1

Your hovering action should be on up, not on the bar itself... 您的悬停动作应该在上面,而不是在酒吧本身上。

something like: 就像是:

$(function(){
var $myel = $('.bar');
 $(".up").hover(function () {
  $myel.stop().animate({
           width: '+=10%'
  }, 350).addClass('shadow');
 }, function () {
 $myel.stop().animate({
     width: '200px'
 }, 350).removeClass('shadow');
});
});

Also you should really consider upgrading your jquery version. 另外,您应该真正考虑升级您的jquery版本。 1.6 is VERY out of date. 1.6非常过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM