如何使JS / Jquery透视效果在模式内部工作?

时间:2019-02-01 00:50:28

标签: javascript jquery html css

如何在下面的模态中实现这种效果?我尝试了很多方法,似乎我缺少了一些东西。当我将所有内容放在“ MODAL CONTENT” div中的.wrap div中时,它不再显示在任何地方。然后,当我更正CSS以正确定位模式 var memberBindings = columnNames.Select(columnName => { var pi = propertiesByName[columnName]; var indexExpr = Expression.MakeIndex(paramExpr, property, new[] { Expression.Constant(columnName) }); //Datarow["columnName"] is of type object, cast to the right type var convert = Expression.Convert(indexExpr, pi.PropertyType); return Expression.Bind(pi, convert); }); 时,内容只是显示为彼此相邻的图像。.我完全不知道为什么会这样吗?有人可以解释我如何在模态示例中完成这项工作吗?

        var memberBindings = columnNames.Select(columnName =>
        {
            try
            {
                var pi = propertiesByName[columnName];
                var indexExpr = Expression.MakeIndex(paramExpr, property,
                    new[] { Expression.Constant(columnName) });
                var convert = Expression.Convert(indexExpr, pi.PropertyType);
                return Expression.Bind(pi, convert);
            }
            catch(Exception e)
            {
                return null;
            }                
        });
        var initExpr = Expression.MemberInit(newExpr, memberBindings.Where(obj => obj != null));
#myModal modal-content .wrap {...
var lFollowX = 0,
    lFollowY = 0,
    x = 0,
    y = 0,
    friction = 1 / 30;

function moveBackground() {
  x += (lFollowX - x) * friction;
  y += (lFollowY - y) * friction;
  
  translate = 'translate(' + x + 'px, ' + y + 'px) scale(1.1)';

  $('.bg').css({
    '-webit-transform': translate,
    '-moz-transform': translate,
    'transform': translate
  });

  window.requestAnimationFrame(moveBackground);
}

$(window).on('mousemove click', function(e) {

  var lMouseX = Math.max(-100, Math.min(100, $(window).width() / 2 - e.clientX));
  var lMouseY = Math.max(-100, Math.min(100, $(window).height() / 2 - e.clientY));
  lFollowX = (20 * lMouseX) / 100; // 100 : 12 = lMouxeX : lFollow
  lFollowY = (10 * lMouseY) / 100;

});

moveBackground();


//////////////////////////////////////////////////MODAL/////////////////////////////////////////////////////////////
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

2 个答案:

答案 0 :(得分:1)

我的猜测是内容以模态显示,但正在被裁剪。尝试为.wrap.modal-content添加特定的高度(注意100% won't work,因为父级和绝对定位的子级没有高度)。

.wrap {
  background-color: #0F2044;
  -webkit-perspective: 100px;
          perspective: 100px;
  position: relative;
  overflow: hidden;
  // Add something like this
  height: 50vh;
}

此外,如果您如上所述尝试使用#myModal modal-content .wrap {...定位,则在.之前缺少类选择器(modal-content)。应该是#myModal .modal-content .wrap {...

答案 1 :(得分:1)

将包装器移动到模式内容内,然后将其制成position:absolute,将模式内容制成position:relative,然后调整z-indexwidth / height

var lFollowX = 0,
  lFollowY = 0,
  x = 0,
  y = 0,
  friction = 1 / 30;

function moveBackground() {
  x += (lFollowX - x) * friction;
  y += (lFollowY - y) * friction;

  translate = 'translate(' + x + 'px, ' + y + 'px) scale(1.1)';

  $('.bg').css({
    '-webit-transform': translate,
    '-moz-transform': translate,
    'transform': translate
  });

  window.requestAnimationFrame(moveBackground);
}

$(window).on('mousemove click', function(e) {

  var lMouseX = Math.max(-100, Math.min(100, $(window).width() / 2 - e.clientX));
  var lMouseY = Math.max(-100, Math.min(100, $(window).height() / 2 - e.clientY));
  lFollowX = (20 * lMouseX) / 100; // 100 : 12 = lMouxeX : lFollow
  lFollowY = (10 * lMouseY) / 100;

});

moveBackground();


//////////////////////////////////////////////////MODAL/////////////////////////////////////////////////////////////
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
body {
  height: 100vh;
}

h1 {
  margin: 0;
  padding: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  color: white;
  font-size: 7vmin;
  text-align: center;
  text-transform: uppercase;
  -webkit-transform: translate3d(-50%, -50%, 0);
  transform: translate3d(-50%, -50%, 0);
}

.wrap {
  background-color: #0F2044;
  -webkit-perspective: 100px;
  perspective: 100px;
  height: 100%;
  width: 100%;
  position: absolute;
  overflow: hidden;
  top:0;
  left:0;
  z-index:-1;
}

.wrap .bg {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}


/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: black;
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content/Box */

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  /* 15% from the top and centered */
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
  position:relative;
  /* Could be more or less, depending on screen size */
  z-index:0;
  color:#fff;
}


/* The Close Button */

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="wrap">
      <div class="bg">
        <img class="front" src="https://shannels.com/fg.svg">
        <div class="bg">
          <img class="front" src="https://shannels.com/mg.svg">
          <div class="bg">
            <img class="front" src="https://shannels.com/bg.svg">
          </div>
        </div>
      </div>
    </div>
    <span class="close">&times;</span>
    <p>MODAL CONTENT</p>
    <p>MODAL CONTENT</p>
    <p>MODAL CONTENT</p>
    <p>MODAL CONTENT</p>
    <p>MODAL CONTENT</p>
    <p>MODAL CONTENT</p>

  </div>
</div>