简体   繁体   English

将 div 从其位置动画到全屏

[英]Animate a div to full screen from its position

I want to expand a div to full screen on clicking on it.我想在单击它时将 div 扩展到全屏。 Just like this Fiddle js link here就像这里的 Fiddle js 链接

I want to animate the same from its position.我想从它的位置动画。 if I click the box it feels like expanding from its position please help me to achieve that如果我单击该框,感觉就像从其位置扩展,请帮助我实现这一目标

 $('.myDiv').click(function(e){ $(this).toggleClass('fullscreen'); });
 .myDiv{background:#cc0000; width:100px; height:100px;float:left:margin:15px;} .myDiv.fullscreen{ z-index: 9999; width: 100%; height: 100%; position: fixed; top: 0; left: 0; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="myDiv"> my div <button>Full Screen</button> </div> <div class="myDiv"> my div 2 <button>Full Screen</button> </div>

Fullscreen animation全屏动画

Now making a element fullscreen is pretty simple.现在制作一个元素全屏非常简单。 It could be done with css alone.它可以单独使用 css 完成。

 .content { display: inline-grid; width: 150px; height: 100px; background-color: cornflowerblue; border-radius: 3px; transition: width 2s, height 2s; margin: 10px; } .content button { display: inline-block; justify-self: center; align-self: center; height: 2em; } .content:hover { width: 100vw; height: 1200vh; }
 <div class="container"> <div class="content"> <button>Fullscreen</button> </div> </div> <div class="content"></div>

Just adding a transition will make the element brake the layout.只需添加一个过渡就会使元素破坏布局。
To not break a layout you need:为了不破坏您需要的布局:

  1. Replace the element.更换元素。 (Below the Visibility : hidden element). (低于可见性:隐藏元素)。
  2. Give it an Absolute position.给它一个绝对位置。
  3. Then set its width to fullscreen and animate its position so it can cover it.然后将其宽度设置为全屏并为其位置设置动画,以便它可以覆盖它。
  4. Added an animation, transition添加了动画,过渡

 //Function is run on page load $(function() { var full = $(".fullscreen"); //Loops over all elements that have the class fullscreen full.each(function(index, elem) { $(elem).click(fullscreenClick); }); function fullscreenClick() { //The button is this //We want to clone the parent var box = $(this).parent(); //create a holder box so the layout stays the same var holder = $(box).clone(false, true); //and make it not visible $(holder).css({ "visibility": "hidden" }); //Get its position var pos = $(box).position(); //Substitute our box with our holder $(box).before($(holder)); //Set the position of our box (not holder) //Give it absolute position (eg. outside our set structure) $(box).css({ "position": "absolute", "left": pos.left + "px", "top": pos.top + "px", }); //Set class so it can be animated $(box).addClass("fullscreen"); //Animate the position $(box).animate({ "top": 0, "left": 0, }, 3000); } });
 * { margin: 0; padding: 0; } .container { display: inline-block; } .container .element { display: inline-block; background-color: cornflowerblue; margin: 5px; padding: 10px; width: 70px; height: 30px; transition: width 3s, height 3s; ; } .container .element.fullscreen { width: calc(100vw - 30px); height: calc(100vh - 30px); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="element"> <button class="fullscreen">Fullscreen</button> </div> <div class="element"> <button class="fullscreen">Fullscreen</button> </div> <div class="element"> <button class="fullscreen">Fullscreen</button> </div> <div class="element"> <button class="fullscreen">Fullscreen</button> </div> <div class="element"> <button class="fullscreen">Fullscreen</button> </div> <div class="element"> <button class="fullscreen">Fullscreen</button> </div> <div class="element"> <button class="fullscreen">Fullscreen</button> </div> <div class="element"> <button class="fullscreen">Fullscreen</button> </div> </div>

You can add animation on all styles changes adding next properties to myDiv class:您可以在所有样式更改上添加动画,将下一个属性添加到myDiv类:

/* Animate all changes */
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;

I will show the changes on your example:我将展示您示例的更改:

 $('.myDiv').click(function(e) { $(this).toggleClass('fullscreen'); });
 .myDiv{ background:#cc0000; width:100px; height:100px; float:left: margin:15px; /*Animations*/ -webkit-transition: all 0.5s ease; -moz-transition: all 0.5s ease; -o-transition: all 0.5s ease; transition: all 0.5s ease; } .myDiv.fullscreen{ z-index: 9999; width: 100%; height: 100%; position: fixed; top: 0; left: 0; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="myDiv"> my div 1 <button>Full Screen</button> </div> <div class="myDiv"> my div 2 <button>Full Screen</button> </div>

It's a bit of a complicated task, but this should give you an idea of how it's done.这是一项复杂的任务,但这应该让您了解它是如何完成的。 This code will run into some issues (clicking quickly will stack setTimeout ) but does the basics.这段代码会遇到一些问题(快速单击会堆叠setTimeout ),但可以解决基础问题。

The idea is that you calculate the current position of the element with getBoundingClientRect() and set initial position values with that, so that when you adjust the position to fixed , it will look as though it's still in the same spot - then when you override those values with the .fullscreen css, the transition property will allow them to animate.这个想法是您使用getBoundingClientRect()计算元素的当前位置并使用它设置初始位置值,这样当您将位置调整为fixed时,它看起来好像仍然在同一个位置 - 然后当您覆盖这些带有.fullscreen css 的值, transition属性将允许它们进行动画处理。

The biggest issue here, which you'll notice if you click on the first div, is that it disappears from the layout and causes the second div to jump up to where it was, you'd probably need a way of preserving the layout.如果单击第一个 div,您会注意到这里最大的问题是它从布局中消失并导致第二个 div 跳到原来的位置,您可能需要一种保留布局的方法。 Hopefully this is helpful as a starting point anyway.希望这无论如何作为一个起点是有帮助的。

 function getPosition(elem){ const rect = elem.getBoundingClientRect() return { top: rect.top, left: rect.left, width: rect.width, height: rect.height } } function toPx(val){ return [val, 'px'].join('') } $('.myDiv').click(function(e){ if(this.classList.contains('fullscreen')){ this.classList.remove('fullscreen') setTimeout(e => this.style.position = 'static', 1000) //close } else { //open let pos = getPosition(this) this.style.width = toPx(pos.width) this.style.height = toPx(pos.height) this.style.top = toPx(pos.top) this.style.left = toPx(pos.left) console.log(pos) this.classList.add('fullscreen') this.style.position = 'fixed' } });
 .myDiv{background:#cc0000; width:100px; height:100px;float:left:margin:15px;} .myDiv.fullscreen{ z-index: 9999; width: 100% !important; height: 100% !important; top: 0 !important; left: 0 !important; } .animateTransitions { transition: all 1s; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="myDiv animateTransitions"> my div <button>Full Screen</button> </div> <div class="myDiv animateTransitions"> my div 2 <button>Full Screen</button> </div>

Here's how i made it:这是我的做法:

  • JQuery: 3.6.0 jQuery:3.6.0
  • Css preprocessor: SCSS CSS预处理器:SCSS
  • CSS Framework: Bootstrap 5.1.0 CSS 框架:Bootstrap 5.1.0

See it in action: https://codepen.io/illegalmexican/pen/NWYNVvg在行动中看到它: https ://codepen.io/illegalmexican/pen/NWYNVvg

Please note that i was answering a JQuery post.请注意,我正在回答一个 JQuery 帖子。 The fowlloing could be made in Vanilla Javascript for better performance. fowlloing 可以用 Vanilla Javascript 制作以获得更好的性能。 Also keep in mind that this could also be improved in terms of Accessibility standars by having a close button.另请记住,这也可以通过关闭按钮在可访问性标准方面得到改善。

Html: html:

<div class="myDiv-Container">
    <div class="row">
        <div class="col-6">
            <div class="d-flex flex-wrap position-relative">
                <div class="myDiv w-50">
                    <div class="front bg-primary top-left">
                        <h1>Hello<br>World</h1>
                    </div>
                </div>
                <div class="myDiv w-50">
                    <div class="front bg-success top-right">
                        <h1>Hello<br>World</h1>

                    </div>
                </div>
                <div class="myDiv w-50">
                    <div class="front bg-danger  bottom-left">
                        <h1>Hello<br>World</h1>
                    </div>
                </div>
                <div class="myDiv w-50">
                    <div class="front bg-warning bottom-right">
                        <h1>Hello<br>World</h1>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

SCSS: SCSS:

.myDiv-Container {
  height: 500px;
  width: 500px;
  position: relative;
}

.front {
  position: relative;
  text-align: center;
  display: flex;

  &.position-absolute {
    z-index: 1;
  }

  &.top-left {
    top: 0;
    left: 0;
  }

  &.bottom-left {
    bottom: 0;
    left: 0;
  }

  &.top-right {
    top: 0;
    right: 0;
  }

  &.bottom-right {
    bottom: 0;
    right: 0;
  }
}

JQuery:查询:

document.addEventListener('DOMContentLoaded', function () {
  jQuery('.myDiv').each(function () {
    var frontElem = jQuery(this).find('.front');
    jQuery(this).on("click", function () {
      if (jQuery(this).hasClass('active')) {
        jQuery(this).removeClass('active');
        jQuery(frontElem).animate(
          {
            width: jQuery(this).width(),
            height: jQuery(this).height(),
          },
          500, function() {
            jQuery(frontElem).removeClass('position-absolute');
            jQuery(frontElem).css({
              width: '100%',
              height: '100%',
            });
          });
      } else {
        jQuery(frontElem).css({
          width: jQuery(this).width(),
          height: jQuery(this).height(),
        });
        jQuery(frontElem).addClass('position-absolute');
        jQuery(this).addClass('active');
        jQuery(frontElem).animate({
          'width': '100%',
          'height': '100%',
        }, 500);
      }
    });
  });   
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM