skip to Main Content

when there are more ‘gy-section’, when you hover over them, only the first photo moves and the rest do not want to work

I tried changing classes, adding other classes, converting them to id, changing the JS code but nothing works.

HTML

<div class="container">
        <div class="gallery">
            <div class="gy-section">
                <div class="gy-sec-img"></div>
            </div>
        </div>
    </div>

JavaScript

let gyHover = this.document.querySelector(".gy-section");
let gySection = this.document.querySelector(".gy-sec-img");
let gyText = this.document.querySelector(".gy-simg-title");

let value = '';

gyHover.addEventListener('mouseenter', async function up() {
  value = gySection.backgroundPositionY;

  gySection.animate([
        // key frames
        { backgroundPositionY: {value} },
        { backgroundPositionY: '100%'}
      ], {
        duration: 4000,
        easing: 'ease-in-out',
        iterations: 1,
    });

    gyHover.classList.add('gys-hover');
    gySection.classList.add('gys-hover');
    gyText.classList.add('gst-hover');
    await new Promise(resolve => setTimeout(resolve, 3999)); // 3 sec
    gySection.style.backgroundPositionY = "100%";
});

gyHover.addEventListener('mouseleave', async function down() {
  value = gySection.backgroundPositionY;

  gySection.animate([
        // key frames
        { backgroundPositionY: {value} },
        { backgroundPositionY: '0%'}
      ], {
        duration: 4000,
        easing: 'ease-in-out',
        iterations: 1,
    });

    gyHover.classList.remove('gys-hover');
    gySection.classList.remove('gys-hover');
    gyText.classList.remove('gst-hover');
    await new Promise(resolve => setTimeout(resolve, 3999)); // 3 sec
    gySection.style.backgroundPositionY = "0%";
});

2

Answers


  1. Chosen as BEST ANSWER

    I fixed it.

        const imgArr = [
        document.getElementById('img-height-1'),
        document.getElementById('img-height-2'),
        document.getElementById('img-height-3'),
        document.getElementById('img-height-4'),
    ];
    
    for(let i = 0; i < imgArr.length; i++) {
        
        imgArr[i].addEventListener('mouseenter', () => {
    
            const imgScrollHeight = imgArr[i].scrollHeight - img.scrollHeight;
    
            console.log('0px ' + imgScrollHeight + 'px');
            imgArr[i].style.translate = '0px -' + imgScrollHeight + 'px';
    
        });
    
        imgArr[i].addEventListener('mouseleave', () => {
    
            const imgScrollHeight = imgArr[i].scrollHeight - img.scrollHeight;
    
            console.log('0px ' + imgScrollHeight + 'px');
            imgArr[i].style.translate = '0px 0px';
    
        });
    
    }
    

    Just have to change JS and add ID to img in html.


  2. Below is a solution which doesn’t utilize JavaScript:

    .gy-section {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-flow: row nowrap;
      margin: 15px;
    }
    
    .gy-sec-img {
      background: url('https://s.imgur.com/images/logo-1200-630.jpg?2');
      width: 50px;
      height: 50px;
      margin: 2px;
      transition: ease-in-out 0.4s;
    }
    
    .gy-sec-img:hover {
      margin-top: 10px;
      margin-bottom: -10px;
    }
    <div class="container">
      <div class="gallery">
        <div class="gy-section">
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
        </div>
        <div class="gy-section">
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
        </div>
        <div class="gy-section">
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
          <div class="gy-sec-img"></div>
        </div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search