2016-11-11 5 views
0

アニメーションを作ろうとしていますが、私の努力はjsfiddleにあります。ここでjQueryで正しくアニメーションを作成するには

/* JavaScript: */ 
 

 
    var app = function() { 
 
     var self = this; 
 
    
 
     var allBoxes = $('.box'); 
 
     var shadow = $('#shadow'); 
 
     var busy = false; 
 
    
 
     self.init = function() { 
 
      self.events(); 
 
     }; 
 
    
 
     self.getBoxLeftPosition = function(id) 
 
     { 
 
      var left = 100; 
 
    
 
      if (id == 'box2') 
 
      { 
 
       left = 300; 
 
      } else if (id == 'box3') 
 
      { 
 
       left = 500; 
 
      } else if (id == 'box4') 
 
      { 
 
       left = 700; 
 
      } 
 
    
 
      return left; 
 
     }; 
 
    
 
     self.events = function() { 
 
    
 
      allBoxes.on('hover mousemove', function(event) { 
 
       event.stopPropagation(); 
 
       var currentBox = $(this); 
 
    
 
       if (currentBox.hasClass('inactive') && !busy) 
 
       { 
 
        busy = true; 
 
        currentBox.removeClass('inactive').addClass('active').animate({ 
 
         left: '-=30', 
 
         width: 260 
 
        }, 400, function() { 
 
         busy = false; 
 
        }); 
 
    
 
        shadow.fadeIn(400); 
 
       } 
 
      }); 
 
    
 
      $('body').mousemove(function(event) { 
 
       var currentBox = $('.active'); 
 
       var leftValue = self.getBoxLeftPosition(currentBox.attr('id')); 
 
    
 
       if (currentBox.length > 0) 
 
       { 
 
        currentBox.stop(); 
 
        currentBox.animate({ 
 
         left: leftValue, 
 
         width: 200 
 
        }, 300, 'swing', function() { 
 
         currentBox.removeClass('active').addClass('inactive'); 
 
        }, 300); 
 
    
 
        shadow.fadeOut(300); 
 
       } 
 
      }); 
 
    
 
     }; 
 
    
 
     return self; 
 
    }; 
 
    
 
    var main = new app(); 
 
    main.init();
/* CSS: */ 
 

 
html, body { 
 
    margin: 0; 
 
} 
 
    
 
.box { 
 
    position: absolute; 
 
    top: 120px; 
 
    width: 200px; 
 
    height: 420px; 
 
} 
 
    
 
.box div { 
 
    text-align: center; 
 
    color: white; 
 
    position: absolute; 
 
    top: 200px; 
 
    left: 0; 
 
    right: 0; 
 
    font-size: 25px; 
 
    font-weight: bold; 
 
} 
 
    
 
#box1 { 
 
    left: 100px; 
 
    background: pink; 
 
} 
 
    
 
#box2 { 
 
    left: 300px; 
 
    background: skyblue; 
 
} 
 
    
 
#box3 { 
 
    left: 500px; 
 
    background: orange; 
 
} 
 
    
 
#box4 { 
 
    left: 700px; 
 
    background: lightgreen; 
 
} 
 
    
 
#shadow { 
 
    display: none; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    background: url('https://lh6.googleusercontent.com/Vz0GTzpQVaxmlIvvGgg64CSxcYBbHzu7gMQERduJ4qjU5HAg8KfisFFQvIqvKL5Vn7LIy6HZ=w1920-h916'); 
 
    z-index: 10; 
 
} 
 
    
 
.inactive { 
 
    z-index: 5; 
 
} 
 
    
 
.active { 
 
    z-index: 20; 
 
}
<!-- HTML: --> 
 

 
<div id="box1" class="box inactive"> 
 
    <div id="copy1">Copy 1</div> 
 
</div> 
 
    
 
<div id="box2" class="box inactive"> 
 
    <div id="copy2">Copy 2</div> 
 
</div> 
 
    
 
<div id="box3" class="box inactive"> 
 
    <div id="copy3">Copy 3</div> 
 
</div> 
 
    
 
<div id="box4" class="box inactive"> 
 
    <div id="copy4">Copy 4</div> 
 
</div> 
 
    
 
<div id="shadow"></div>

は、私は言葉で達成しようとしているものです:私は4箱を持っており、ユーザーはニーズをボックスそのうちの一つの上に置いたときに、ここで私が使用したコードです少し拡大すると他のものはグレー表示にする必要があり、マウスがボックスを出るたびに元の状態に戻る必要があります。

私のjsfiddleでは、私は一点まで作業しましたが、それはバグです。

答えて

1

と同じ効果を得ることができますの中にprev sibling selectorというようなものがあります。ホバリングですぐに変わるz-indexのためにちょっと跳ね上がっています。

+0

それは私が必要なものに近いですが、私は本当に#shadow要素が必要ですポップインとは、ページにgreydの必要がある他のものがあるからです。 –

+0

ポップインとは何を意味しますか?あなたはモーダルのような意味ですか? –

+0

はい、あなたは現在、ホバリングしているボックスとは別にページ全体をグレイズしています。 –

1

あなたはこのJSFiddle

$('.box').hover(function(){ 
$(this).siblings().addClass('inactive'); 
}, function(){ 
$(this).siblings().removeClass('inactive'); 
}); 

純粋なCSSでそれを実行しようとしましたが、悲しいことなしがありますを見ても、CSS

$(document).on('mouseenter', '.item', function(e){ 
 
    var me = $(this); 
 
    $('.item').not(this).addClass('greyed'); 
 
}); 
 

 
$(document).on('mouseleave', '.item', function(e){ 
 
    $('.item').removeClass('greyed'); 
 
});
ul,li{ 
 
list-style:none;padding:0;margin:0; 
 
} 
 
ul{ 
 
    width:400px; 
 
} 
 
li, .item { 
 
    width:100px; 
 
    height:100px; 
 
} 
 
li { 
 
    float:left; 
 
    position:relative; 
 
} 
 
.item { 
 
    background-color: #eee; 
 
    border:1px solid #ccc; 
 
    position:absolute; 
 
    z-index:1; 
 
    -webkit-transition:all 0.3s ease-in-out; 
 
} 
 
.item:hover { 
 
    width:110px; 
 
    z-index:2; 
 
} 
 

 
.red{ 
 
    background: red; 
 
} 
 

 
.pink{ 
 
    background: pink; 
 
} 
 

 
.blue{ 
 
    background: blue; 
 
} 
 

 
.yellow{ 
 
    background: yellow; 
 
} 
 

 
.greyed{ 
 
    -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */ 
 
    filter: grayscale(100%); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> 
 
<ul> 
 
    <li> 
 
     <div class="item red"></div> 
 
    </li> 
 
    <li> 
 
     <div class="item blue"></div> 
 
    </li> 
 
    <li> 
 
     <div class="item yellow"></div> 
 
    </li> 
 
    <li> 
 
     <div class="item pink"></div> 
 
    </li> 
 
    
 
</ul>

+0

なぜ**のみ**接頭辞バージョン? – Kaiido

+0

これはバグです、それは私が欲しいものを達成していません –

+0

ああ..私は灰色の部分を忘れました...私はコードを更新します – mrid

1

ここに私がしたことがあります。

まず、CSSの背景画像が有効なリソースではないことがわかりましたので、通常の背景色に置き換えました(透明な黒ですが、調整したいかもしれません)。

第2に、allBoxes.on('hover mousemove'allBoxes.on('mouseover'に、$('body').mousemoveallBoxes.on('mouseout'に変更しました。

第3に、フラグトラッキング$busyを削除しました。

第4に、var currentBox = $('.active');var currentBox = $(event.target);に変更しました。

var app = function() { 
 
    var self = this; 
 

 
    var allBoxes = $('.box'); 
 
    var shadow = $('#shadow'); 
 
    var busy = false; 
 

 
    self.init = function() { 
 
     self.events(); 
 
    }; 
 

 
    self.getBoxLeftPosition = function(id) 
 
    { 
 
     var left = 100; 
 

 
     if (id == 'box2') 
 
     { 
 
      left = 300; 
 
     } else if (id == 'box3') 
 
     { 
 
      left = 500; 
 
     } else if (id == 'box4') 
 
     { 
 
      left = 700; 
 
     } 
 

 
     return left; 
 
    }; 
 

 
    self.events = function() { 
 

 
     allBoxes.on('mouseover', function(event) { 
 
      event.stopPropagation(); 
 
      var currentBox = $(this); 
 

 
      if (currentBox.hasClass('inactive') && !busy) 
 
      { 
 
       //busy = true; 
 
       currentBox.removeClass('inactive').addClass('active').animate({ 
 
        left: '-=30', 
 
        width: 260 
 
       }, 400, function() { 
 
        //busy = false; 
 
       }); 
 

 
       shadow.fadeIn(400); 
 
      } 
 
     }); 
 

 
     allBoxes.on('mouseout', function(event) { 
 
      var currentBox = $(event.target); 
 
      var leftValue = self.getBoxLeftPosition(currentBox.attr('id')); 
 

 
      if (currentBox.length > 0) 
 
      { 
 
       currentBox.stop(); 
 
       currentBox.animate({ 
 
        left: leftValue, 
 
        width: 200 
 
       }, 300, 'swing', function() { 
 
        currentBox.removeClass('active').addClass('inactive'); 
 
       }, 300); 
 

 
       shadow.fadeOut(300); 
 
      } 
 
     }); 
 

 
    }; 
 

 
    return self; 
 
}; 
 

 
var main = new app(); 
 
main.init();
html, body { 
 
    margin: 0; 
 
} 
 

 
.box { 
 
    position: absolute; 
 
    top: 120px; 
 
    width: 200px; 
 
    height: 420px; 
 
} 
 

 
.box div { 
 
    text-align: center; 
 
    color: white; 
 
    position: absolute; 
 
    top: 200px; 
 
    left: 0; 
 
    right: 0; 
 
    font-size: 25px; 
 
    font-weight: bold; 
 
} 
 

 
#box1 { 
 
    left: 100px; 
 
    background: pink; 
 
} 
 

 
#box2 { 
 
    left: 300px; 
 
    background: skyblue; 
 
} 
 

 
#box3 { 
 
    left: 500px; 
 
    background: orange; 
 
} 
 

 
#box4 { 
 
    left: 700px; 
 
    background: lightgreen; 
 
} 
 

 
#shadow { 
 
    display: none; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 0; 
 
    width: 1000px; 
 
    height: 600px; 
 
    
 
    /*background: url('https://lh6.googleusercontent.com/Vz0GTzpQVaxmlIvvGgg64CSxcYBbHzu7gMQERduJ4qjU5HAg8KfisFFQvIqvKL5Vn7LIy6HZ=w1920-h916');*/ 
 
    background-color: rgba(0,0,0,0.5); /* transparent black */ 
 
    
 
    z-index: 10; 
 
} 
 

 
.inactive { 
 
    z-index: 5; 
 
} 
 

 
.active { 
 
    z-index: 20; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 

 
<div id="box1" class="box inactive"> 
 
    <div id="copy1">Copy 1</div> 
 
</div> 
 

 
<div id="box2" class="box inactive"> 
 
    <div id="copy2">Copy 2</div> 
 
</div> 
 

 
<div id="box3" class="box inactive"> 
 
    <div id="copy3">Copy 3</div> 
 
</div> 
 

 
<div id="box4" class="box inactive"> 
 
    <div id="copy4">Copy 4</div> 
 
</div> 
 

 
<div id="shadow"></div>

+0

これはバグです!item1をホバリングしてすぐに他のアイテムに移動して何度もやりましょう –

+0

まだまだバグです –

+0

oops、私の悪い。私はもう一度やり直すよ... – elfan

関連する問題