如何在点击时交换网格中的元素而无需重新加载DOM

时间:2019-04-26 17:42:10

标签: javascript jquery css flexbox css-grid

https://codepen.io/anon/pen/QPYppp?&editable=true

jQuery.fn.swap = function(b) {
  b = jQuery(b)[0];
  var a = this[0],
    a2 = a.cloneNode(true),
    b2 = b.cloneNode(true),
    stack = this;

  a.parentNode.replaceChild(b2, a);
  b.parentNode.replaceChild(a2, b);

  stack[0] = a2;
  return this.pushStack(stack);
};

function changeVideos() {
  $('.wrap-grid > div:nth-child(n + 2)').click(function() {
    $('.wrap-grid > div:nth-child(1)').swap($(this));
    $(this).off();
    changeVideos();
  })
};
changeVideos();
*,
::after,
::before {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.main-content .container>.row> :first-child {
  padding-left: 0;
}

.main-content .container>.row> :last-child {
  padding-right: 0;
}

.wrap-grid {
  width: 100%;
  display: grid;
  grid-gap: 10px;
  grid-auto-columns: 1fr 1fr;
  grid-template-areas: "a b" "a c" "a d";
  margin-bottom: 20px;
}

.wrap-grid>div {
  position: relative;
}

.wrap-grid iframe {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
}

.wrap-grid>* {
  width: 100%;
  background: #fff;
}

.wrap-grid> :nth-child(n + 2) iframe {
  pointer-events: none;
}

.wrap-grid> :nth-child(1) {
  grid-area: a;
  padding-top: 56.25%;
}

@media (max-width: 991.98px) {
  .wrap-grid {
    grid-template-areas: "a" "b" "c" "d";
  }
  .wrap-grid>* {
    height: 255px;
  }
  .wrap-grid> :nth-child(1) {
    height: 255px;
  }
  .main-content .container>.row> :first-child,
  .main-content .container>.row> :last-child {
    padding-left: 0;
    padding-right: 0;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap-grid">
  <div><iframe width="560" height="315" src="https://www.youtube.com/embed/Bey4XXJAqS8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
  <div><iframe width="560" height="315" src="https://www.youtube.com/embed/Bey4XXJAqS8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
  <div><iframe width="560" height="315" src="https://www.youtube.com/embed/Bey4XXJAqS8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
  <div><iframe width="560" height="315" src="https://www.youtube.com/embed/Bey4XXJAqS8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>

</div>

grid image example

我试图将右侧的三个元素之一与左侧的元素交换。与示例中的示例相同,我尝试与jquery进行交换,但这是重新加载dom元素,并且它们正在闪烁。是否可以通过操作order中的flexbox之类的属性或使用grid布局来实现此目的。

2 个答案:

答案 0 :(得分:0)

您可以使用普通的JavaScript访问并设置其Flexbox order

document.getElementById('your-id').style.order = 2
document.getElementById('your-id').style.order // returns "2"

或jQuery:

$('#your-id').css('style', 2)
$('#your-id').css('style') // returns "2"

答案 1 :(得分:0)

一件事是,您正在对视频进行格式化并将视频加载到iframe中,而在移动iframe时必须重新加载。

我建议修改代码,以便您加载基本图像,因为与完整的youtube视频相比,它们无需花费时间重新加载。

youtube视频的封面图片是

 <img class="youtube-thumb" src="//i.ytimg.com/vi/' + id + '/hqdefault.jpg">

对于您的视频,它将是:

   <img class="youtube-thumb" src="//i.ytimg.com/vi/Bey4XXJAqS8/hqdefault.jpg">