繁体   English   中英

如何在js中将div置于最前面,调整大小并将其居中到屏幕中间?

[英]How bring div to front, resize and center it to the middle of the screen onclick in js?

请耐心等待,我很难为此想出一个好的解释。

所以我想制作一个信息页面,其中有几个 div 显示它所包含的信息的一些预览。 像这样:标准视图

当访问者单击其中一个 div 时,它会翻转、增大大小并居中在所有其他 div 的顶部,而不会影响它们。 像这样:

单击 div 时

到目前为止,我已经完成了网格结构和翻转,但是一旦我开始摆弄 div 的尺寸,一切都会移动:

https://jsfiddle.net/xdqwL807/

 $(document).on("click", ".flip-container", function () { $(this).toggleClass('hover'); $('.front').removeClass('front'); $(this).toggleClass('zIndex') });
 .flip-container { perspective: 1000px; } .flip-container.hover .flipper { transform: rotateY(180deg); } .bringToFront { width: 500px !important; height: 500px !important; align-content: center !important; z-index: 1000 !important; } .flip-container, .front, .back { display: inline-flex; width: 320px; height: 480px; } .flipper { transition: 0.6s; transform-style: preserve-3d; position: relative; } .front, .back { backface-visibility: hidden; position: absolute; top: 0; left: 0; } .front { z-index: 2; transform: rotateY(0deg); } .back { transform: rotateY(180deg); } .green { width: 320px; height: 480px; background-color: green; } .blue { background-color: blue; width: 320px; height: 480px; }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> </head> <body> <div style="display: flexbox; align-content: center; width: 100%; position:absolute"></div> <div class="flip-container"> <div class="flipper"> <div class="front"> <!-- front content --> <div class="green"> </div> </div> <div class="back"> <!-- back content --> <div class="blue"> </div> </div> </div> </div> <div class="flip-container"> <div class="flipper"> <div class="front"> <!-- front content --> <div class="green"> </div> </div> <div class="back"> <!-- back content --> <div class="blue"> </div> </div> </div> </div> </body> <script> </script> </html>

您需要使用 CSS position 来实现所需的结果。 所以这里是我对你的代码所做的修改:

为每个.flip-container添加了一个包装器.card-holder ,这对于避免元素移动很重要,因为.flip-container位置是固定的,这样其他元素就不会移动,因为.card-holder将在那里以免移位。

<div class="card-holder">
  <div class="flip-container">
    <div class="flipper">
      <div class="front">
        <!-- front content -->
        <div class="green">
        </div>
      </div>
      <div class="back">
        <!-- back content -->
        <div class="blue">
        </div>
      </div>
    </div>
  </div>
</div>

将此 CSS 添加到.card-holder

.card-holder {
  width: 320px;
  height: 480px;
  display: inline-flex;
}

并从.flip-container, .front, .back删除以下.flip-container, .front, .back

.flip-container, .front, .back {
  width: 320px;
  height: 480px;
}

在 CSS 中添加了以下动画,在焦点动画上,卡片将被固定并为 2 倍大小,在模糊时它将处于其初始状态。

.focus {
  animation: focus 0.4s ease-in-out forwards;
}

.blur {
  animation: blur 0.4s ease-in-out forwards;
}

@keyframes focus {
  to {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(2);
    z-index: 5;
  }
}

@keyframes blur {
  from {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(2);
    z-index: 5;
  }
}
  to {
    position: relative;
    top: initial;
    left: initial;
    transform: translate(0%, 0%) scale(1);
    z-index: 0;
  }
}

这是我添加的 jquery 代码:

if((!$(this).hasClass('focus') && !$(this).hasClass('blur')) || $(this).hasClass('blur')) {  
  // add focus and remove blur class for initial state as well as if blur class is available
  $(this).addClass('focus');
  $(this).removeClass('blur');
}
else if ($(this).hasClass('focus')) {
  // add blur and remove focus class if focus class is available
  $(this).addClass('blur');
  $(this).removeClass('focus');
}
// to stop event propagation to other elements
event.stopPropagation()

看看它在行动: https : //jsfiddle.net/8jh4pwut/15/

 $(document).on("click", ".flip-container", function (event) { $(this).toggleClass('hover'); $('.front').removeClass('front'); $(this).toggleClass('zIndex'); if((!$(this).hasClass('focus') && !$(this).hasClass('blur')) || $(this).hasClass('blur')) { $(this).addClass('focus'); $(this).removeClass('blur'); } else if ($(this).hasClass('focus')) { $(this).addClass('blur'); $(this).removeClass('focus'); } event.stopPropagation() });
 .flip-container { perspective: 1000px; } .flip-container.hover .flipper { transform: rotateY(180deg); } body { margin: 0; } .bringToFront { width: 500px !important; height: 500px !important; align-content: center !important; z-index: 1000 !important; } .card-holder { width: 320px; height: 480px; display: inline-flex; } .flip-container, .front, .back { display: inline-flex; } .flipper { transition: 0.6s; transform-style: preserve-3d; position: relative; } .front, .back { backface-visibility: hidden; position: absolute; top: 0; left: 0; } .front { z-index: 2; transform: rotateY(0deg); } .back { transform: rotateY(180deg); } .green { width: 320px; height: 480px; background-color: green; } .blue { background-color: blue; width: 320px; height: 480px; } .focus { animation: focus 0.4s ease-in-out forwards; } .blur { animation: blur 0.4s ease-in-out forwards; } @keyframes focus { to { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(2); z-index: 5; } } @keyframes blur { from { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(2); z-index: 5; } } to { position: relative; top: initial; left: initial; transform: translate(0%, 0%) scale(1); z-index: 0; } }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> </head> <body> <div class="overlay"> </div> <div style="display: flexbox; align-content: center; width: 100%; position:absolute"></div> <div class="card-holder"> <div class="flip-container"> <div class="flipper"> <div class="front"> <!-- front content --> <div class="green"> </div> </div> <div class="back"> <!-- back content --> <div class="blue"> </div> </div> </div> </div> </div> <div class="card-holder"> <div class="flip-container"> <div class="flipper"> <div class="front"> <!-- front content --> <div class="green"> </div> </div> <div class="back"> <!-- back content --> <div class="blue"> </div> </div> </div> </div> </div> </body> <script> </script> </html>

最好和最简单的方法是使用 JavaScript。 解释在代码片段下方。

 function hello() { document.getElementById('div').classList.add('anim'); setTimeout(function(){ document.getElementById('div').classList.remove('anim'); }, 1000); }
 #div{ height: 100px; width: 100px; background-color: yellow; } .anim { animation: anim 1s; } @keyframes anim { to { background-color: red; } }
 <div id="div" onclick="hello()"></div>

现在,在CSS 中,我们创建了具有一些样式的 div,并创建了一个具有动画样式的单独类 然后像往常一样,我们将关键帧规则添加到动画中。

然后在我们的JS 中,我们有一个函数可以将该类添加到我们的 div 中。 然后我们设置一个超时函数,该函数将时间指定为以毫秒为单位的动画长度,这将从 div 中删除动画类。

然后在HTML 中,我们指定hello()函数作为我们 div 的onclick属性。 就是这样。

如果你对动画感兴趣,你可以访问我的博客,特别是这个只讨论这种技术的博客

暂无
暂无

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

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