Expand / shrink div on hover / out with jQuery

后端 未结 5 597
野趣味
野趣味 2020-12-25 12:30

I am looking for a jQuery plugin to expand div elements so as to reveal their overflow (if any) on hover. Illustration:

5条回答
  •  旧时难觅i
    2020-12-25 13:15

    If it is text, it is a little more complicated...

    I use it like this:

    $('.floating').mouseenter(function(){
      const $this = $(this);
      const dimension = $this.data('dimension');
      const ratioEnlarged = 2;
    
      const tempElement = $this.clone();
      tempElement.appendTo('body');
      tempElement.css({
        width: dimension.width,
        height: dimension.height
      });
    
      if(tempElement.is(':offscreen')){
        // Change this to animate if you want it animated.
        $this.css({
          'margin-left': -dimension.width * ratioEnlarged/2,
          'margin-top': -dimension.height * ratioEnlarged/4,
          'font-size': ratioEnlarged + 'em',
          width: dimension.width * ratioEnlarged,
          height: dimension.height * ratioEnlarged
        });
      } else {
        $this.css({
          'margin-left': -dimension.width * ratioEnlarged/4,
          'margin-top': -dimension.height * ratioEnlarged/4,
          'font-size': ratioEnlarged + 'em',
          width: dimension.width * ratioEnlarged,
          height: dimension.height * ratioEnlarged
        });
      }
    
      tempElement.remove();
    });
    
    $('.floating').mouseleave(function(event) {
      const $this = $(this);
      const dimension = $this.data('dimension');
    
      if(!$this.hasClass('context-menu-active')){
        $this.css({
          margin: 0,
          'font-size': '1em',
          width: dimension.width,
          height: dimension.height
        });
      }
    });
    

提交回复
热议问题