2016-11-11 2 views
0

Ich versuche, eine Animation zu machen, können Sie meine Bemühungen bei jsfiddle sehen. Und hier ist der Code, den ich verwendet habe:Wie mache ich diese Animation richtig mit 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>

Hier ist, was ich in Worten zu erreichen versuchen: Ich habe 4 Boxen und wenn ein Benutzer über einen von ihnen schwebt, die Bedürfnisse Box um etwas zu erweitern und alles andere muss ausgegraut sein und wenn die Maus die Box verlässt, muss sie in ihren ursprünglichen Zustand zurückkehren.

In meiner jsfiddle habe ich es funktioniert zu einem Punkt, aber es ist fehlerhaft.

Antwort

1

an diesem JSFiddle einen Blick

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

versuchte es mit reinem CSS zu tun, aber leider gibt es keine so etwas wie prev sibling selector in css. Es ist ein bisschen nervös wegen z-index bekommen sofort ändern bei Hover.

+0

Das ist nah an dem, was ich brauche, aber ich brauche wirklich das #Shadow-Element zu Pop-in, weil es andere Sachen auf der Seite gibt, die greyd out sein müssen. –

+0

was willst du machen? Du meinst wie ein Modal? –

+0

Ja, wie ein Modal, das die ganze Seite ausgraut, abgesehen von der Box, in der Sie gerade schweben –

1

können Sie den gleichen Effekt erzielen auch mit 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

warum ** nur ** die Präfix-Version? – Kaiido

+0

Dies ist auch fehlerhaft, es erreicht nicht, was ich will –

+0

Oh..Ich habe den grauen Teil vergessen ... Ich werde den Code aktualisieren – mrid

1

Hier ist was ich getan habe.

Zuerst fand ich, dass das Hintergrundbild in Ihrem CSS keine gültige Ressource ist, also ersetzte ich es durch reguläre Hintergrundfarbe (es ist transparent schwarz, Sie möchten es vielleicht anpassen).

Zweitens änderte ich allBoxes.on('hover mousemove' zu allBoxes.on('mouseover' und $('body').mousemove zu allBoxes.on('mouseout'.

Drittens entfernte ich die $busy Flag-Tracking.

Viertens habe ich var currentBox = $('.active'); zu var currentBox = $(event.target); geändert.

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

das ist Buggy! Versuchen Sie, auf item1 zu schweben und sofort auf andere Elemente zu schweben und es viele Male zu tun. –

+0

Ja, es ist immer noch fehlerhaft. –

+0

oops, ich werde es wieder versuchen ... – elfan