Search code examples
javascriptjqueryhtmlcssweb-frontend

"Image masking" thumbnail + full size image. Tricky task


I need to set up an effect like this one:

http://kaceymorrow.com/slo-fi/#Julia_Audio

When you hover an image in that section, the image change its size to cover most of the screen but notice how the transition is very smooth: The large image's position coincides exactly with the thumbnail image, so it looks like the image "uncover" itself, instead of expanding.

I've tried several things with no success. I have nothing near the solution.

EDIT:

This is the best I have but again, not near the solution!

This script waits for hover on an image to display a DIV with the image as a background. Problem is, it seems like the image is expanding not "uncovering".

$(document).ready(function(){
    $('.ejemplo').hover(function(){
        console.log('hoveeeeer');
        var img = $(this).data('img');
        console.log(img);
        $('.c1').css(
            'background-image', 'url('+img+')',
        )
        $('.c1').show()
        $('.c1').css(
            'opacity', '1'
        )
        $('.ejemplo').css(
            'opacity', '0'
        )
        $('.header_highlights').css(
            'opacity', '0'
        )

        var audio1 = $(this).data('audio');
        var stereo = document.getElementById(audio1);
      //Example of an HTML Audio/Video Method
        stereo.play();
    }).mouseout(function(){
    console.log('no hover')
    $('.c1').hide()
    $('.c1').css(
        'opacity', '0'
    )
    $('.ejemplo').css(
        'opacity', '1'
    )
    $('.header_highlights').css(
        'opacity', '1'
    )
    var audio1 = $(this).data('audio');
    var stereo = document.getElementById(audio1);
    stereo.pause();
    });
  });

HTML:

  <div class="container-flex w-container" style="">
        <?php $behind_the_image_images = get_field( 'behind_the_image' ); $count= 0;?>
        <?php if ( have_rows( 'behind_the_image' ) ) : ?>
    <?php while ( have_rows( 'behind_the_image' ) ) : the_row(); ?>
           <div data-w-id="50eadee3-1406-6c5c-563f-0957896a13d2" style="opacity:0" class="div-block-5">

   <a id="hover-<?=$count?>" href="#"  class="link-block-4 _1 link-<?=$count?> w-inline-block ejemplo" style="position:relative;" data-audio="audio-<?=$count?>" data-img="<?= get_sub_field('image'); ?>"></a>

            <div class="header_highlights _2"><?= get_sub_field( 'title_image' ) ?></div>
            <style>.link-<?=$count?> {background-image:url('<?= get_sub_field( 'image' ); ?>')!important;}</style>
                    <audio id="audio-<?=$count?>">
              <source src="<?= get_sub_field('audio_hover'); ?>" type="audio/mpeg"></source>
              <source src="nav.ogg" type="audio/ogg"></source>
            </audio>
          </div>
          <?php $count++; ?>
            <?php endwhile; ?>
        <?php endif; ?>
        </div>
      </div>
      <div style="" class="c1"></div>
      </div>

Solution

  • I ended up solving it trough clip-path and jQuery.

    JS:

    jQuery(document).ready(function(){
      jQuery(".bimg").hover(function(){
        jQuery(".bimg").attr("style","clip-path: inset(0 0 0 0);");
        }, function(){
        jQuery(".bimg").attr("style","clip-path: inset(30% 60% 35% 12%);");
      });
    });
    

    CSS:

    .bimg {
    max-width:none;
    position:absolute;
    left:-60%;
    padding:0px;
    margin: 0%;
    top:-650%;
    }
    

    HTML

    <img src="<?= get_sub_field('image'); ?>"  width="1000px;" style="-webkit-clip-path: inset(30% 60% 35% 12%);clip-path: inset(30% 60% 35% 12%);" height="650px;" class="bimg">
    

    The image doesn't show perfectly, I need to add more CSS to display the image exactly as I want and I need to make it cross-browser compatible and responsive, but the basic idea is here.