Плавное наведение JS

176
15 ноября 2018, 18:50

Есть галерея,при наведении на картинку должен появляться hover с текстом,должно это все выглядеть как ТУТ когда наводишь на картинку он очень плавно появляется затемнение с текстом. Я взяла у них скрипт,изменила в js только классы,но у меня почему-то не работает. Подскажите где я допускаю ошибку. Ниже привела пример кода. Так же можно глянуть Тут

/** 
        Gets the direction you are moving into ( or out of) an element from. 
        0 = top 
        1 = right 
        2 = bottom 
        3 = left 
    */ 
function getDirection($el, coordinates) { 
 
  // the width and height of the current div 
  var w = $el.width(), 
    h = $el.height(), 
 
    // calculate the x and y to get an angle to the center of the div from that x and y. 
    // gets the x value relative to the center of the DIV and "normalize" it 
    x = (coordinates.x - $el.offset().left - (w / 2)) * (w > h ? (h / w) : 1), 
    y = (coordinates.y - $el.offset().top - (h / 2)) * (h > w ? (w / h) : 1), 
 
    // the angle and the direction from where the mouse came in/went out clockwise (TRBL=0123); 
    // first calculate the angle of the point, 
    // add 180 deg to get rid of the negative values 
    // divide by 90 to get the quadrant 
    // add 3 and do a modulo by 4  to shift the quadrants to a proper clockwise TRBL (top/right/bottom/left) **/ 
    direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4; 
 
  return direction; 
 
} 
 
var eventHandler = function() { 
  $(this).click(function(e) { 
    e.preventDefault(); 
  }); 
 
  $(this).mouseenter(function(event) { 
 
    width = $(this).width(); 
    height = $(this).height(); 
    $(this).find('.portfolio-content').css('display', 'block'); 
    $(this).find('.portfolio-content').css('width', width); 
    $(this).find('.portfolio-content').css('height', height); 
    direction = getDirection($(this), { 
      x: event.pageX, 
      y: event.pageY 
    }) 
 
    switch (direction) { 
      case 0: 
        $(this).find('.portfolio-content').animate({ 
          marginTop: "-" + height + "px", 
          marginLeft: "0" 
        }, 0) /* move into position */ 
        $(this).find('.portfolio-content').animate({ 
          marginTop: "0", 
          marginLeft: "0" 
        }, 200) /* show overlay  */ 
        break; 
      case 1: 
        $(this).find('.portfolio-content').animate({ 
          marginTop: "0", 
          marginLeft: width + "px" 
        }, 0) /* move into position */ 
        $(this).find('.portfolio-content').animate({ 
          marginTop: "0", 
          marginLeft: "0" 
        }, 200) /* show overlay  */ 
        break; 
      case 2: 
        $(this).find('.portfolio-content').animate({ 
          marginTop: height + "px", 
          marginLeft: "0" 
        }, 0) /* move into position */ 
        $(this).find('.portfolio-content').animate({ 
          marginTop: "0", 
          marginLeft: "0" 
        }, 200) /* show overlay  */ 
        break; 
      case 3: 
        $(this).find('.portfolio-content').animate({ 
          marginTop: "0", 
          marginLeft: "-" + width + "px" 
        }, 0) /* move into position */ 
        $(this).find('.portfolio-content').animate({ 
          marginTop: "0", 
          marginLeft: "0" 
        }, 200) /* show overlay  */ 
        break; 
    } 
  }); 
 
  $(this).mouseleave(function(event) { 
    width = $(this).width(); 
    height = $(this).height(); 
    $(this).find('.portfolio-content').css('display', 'none'); 
    $(this).find('.portfolio-content').css('height', 0); 
  }); 
} 
 
function updateGridHover() { 
  $('.portfolio-content').animate({ 
    marginTop: "-1000000px", 
    marginLeft: "-1000000px" 
  }, 0) 
 
  $('#grid-container .portfoli').each(eventHandler); 
}
.container-fluid-gallery { 
  max-width: 1800px; 
  padding: 0; 
} 
 
.portfolio { 
  position: relative; 
} 
 
.portfolio-content { 
  position: absolute; 
  color: #fff; 
  padding: 18px; 
  top: 0; 
  left: 0; 
  z-index: 9999; 
  height: 100%; 
  width: 100%; 
  background: none repeat scroll 0px 0px #6170ca; 
} 
 
.portfolio-content_clickable { 
  cursor: pointer; 
}
<section class="portfolio-area"> 
  <div class="container-fluid container-fluid-gallery"> 
    <div class="grid"> 
      <div class="grid-item logo"> 
        <div class="portfolio"> 
          <a href=""><img src="https://i.stack.imgur.com/FzjGG.jpg" alt=""></a> 
          <div class="portfolio-content portfolio-content_clickable"> 
            <h3 class="portfolio-content-h3">Lorem ipsum</h3> 
            <p class="sportfolio-content-p">Lorem ipsum dolor sit amet, consectetur adipiscing</p> 
          </div> 
        </div> 
      </div> 
      <div class="grid-item grid-item__w2 grid-itemlogo"> 
        <div class="portfolio"> 
          <a href=""><img src="https://i.stack.imgur.com/iUfno.jpg" alt=""></a> 
          <div class="portfolio-content portfolio-content_clickable"> 
            <h3 class="portfolio-content-h3">Lorem ipsum</h3> 
            <p class="sportfolio-content-p">Lorem ipsum dolor sit amet, consectetur adipiscing</p> 
          </div> 
        </div> 
      </div> 
    </div> 
  </div> 
</section>

Answer 1

Это проще сделать на CSS. На JS нужно делать если прям точь в точь копировать их подход, конкретно в том что бы понимать откуда на картинку вошла мышь и менять направление анимации. Если это опустить то лучше собрать все на CSS.

.box{ 
  display: flex; 
  justify-content: center; 
  flex-wrap: wrap; 
} 
.part{ 
  width: 50%; 
  position: relative; 
} 
img{ 
  width: 100%; 
  height: 100%; 
} 
.part:hover .shadow{ 
  display: block; 
  animation: left ease 0.5s forwards; 
} 
.part:hover .desc{ 
  display: block; 
  animation: left ease 0.5s forwards; 
} 
.desc{ 
  width: 100%; 
  text-align: center; 
  text-transform: uppercase; 
  font-size: 22px; 
  color: white; 
  position: absolute; 
  bottom: 20px; 
  z-index: 2; 
  display: none; 
} 
.shadow{ 
  position: absolute; 
  width: 100%; 
  height: 100%; 
  left: 0; 
  top: 0; 
  margin: 0 auto; 
  background: rgba(0,0,0,0.8); 
  display: none; 
} 
 
@keyframes left{ 
  from { 
    margin-left: -100%; 
  } 
 
  to { 
    margin-left: 0%; 
  } 
}
<div class="box"> 
  <div class="part"> 
     <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS9pKhNdORp7GaKjJUTFPfZwZfURmKVFiK5dWKn8eJQM5J40HHlaw" alt="">  
     <a href="#" class="desc">Мадам</a> 
     <p class="shadow"></p> 
  </div> 
  <div class="part"> 
    <img src="https://cdn4.techly.com.au/wp-content/uploads/2018/03/techly-smartphone-camera-noise-799x423.jpg" alt=""> 
    <a href="#" class="desc">Хацкер</a> 
    <p class="shadow"></p> 
  </div> 
  <div class="part"> 
    <img src="https://cdn.theatlantic.com/assets/media/img/photo/2016/02/smithsonian-magazines-2015-photo-co/s01_GeertWeggen/main_900.jpg?1456418926" alt=""> 
    <a href="#" class="desc">Мышка в домике</a> 
    <p class="shadow"></p> 
  </div> 
  <div class="part"> 
    <img src="https://iso.500px.com/wp-content/uploads/2016/02/stock-photo-114337435-1500x1000.jpg" alt=""> 
    <a href="#" class="desc">Follow Me</a> 
    <p class="shadow"></p> 
  </div> 
</div>

READ ALSO
Печать заголовка на каждой странице

Печать заголовка на каждой странице

Проблема с печатной формойПисьмо которое формируется динамически

132
При клике записать значение в input

При клике записать значение в input

Есть class="quantity" в этот класс должны приходить значения количество детей у которых выбран возрастВ class="number_val" возраст ребенка, а в class="input_val"...

185
Отступы вокруг элемента

Отступы вокруг элемента

Добавил на пустую страницу блок размером 400х400, нужно чтобы его начало координат было с самого угла, но он вокруг себя делает отступы как на скринеКод...

153