小编Ser*_*han的帖子

如何使用 JavaScript 获取元素的网格坐标?

假设我有一个 3 列的 CSS 网格。有没有一种方法,使用JavaScript,以获得grid-rowgrid-column自动放置元素?

例子:

console.log($('#test').css('grid-row'), $('#test').css('grid-column'));
// expected output: 2 3 
// actual output: two empty strings
Run Code Online (Sandbox Code Playgroud)
.grid {
  display: grid;
  grid-template-columns: repeat( 3, 1fr);
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div id="test"></div>
  <div></div>
  <div></div>
  <div></div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是示例的 JSFiddle:https ://jsfiddle.net/w4u87d2f/7/

在这个例子中,我可以通过计算元素并知道网格有三列来计算:

grid-column = $('#test').index() % 3 + 1;
grid-row = Math.ceil( $('#test').index() / 3 )
Run Code Online (Sandbox Code Playgroud)

但这仅适用于非常简单的网格,也意味着我必须考虑更改列数的断点。

编辑:这不是Retrieve the position (X,Y) of an HTML element的副本,因为我对像素坐标不感兴趣,但对 CSS-Grid 中的行号和列号不感兴趣。

javascript css jquery css-grid

7
推荐指数
2
解决办法
7974
查看次数

Owl carousel 的 .destroy() 删除不相关的 div

我想根据视口的宽度在轮播视图和不同的布局之间切换。设置轮播效果很好。当我想删除它时遇到了问题。

$owlTeam.destroy();根据文档使用which 应该在初始化轮播之前重新创建标记的状态,但由于某种原因删除了两个意想不到的关键 div。一个是包装 div,它是包含轮播的 div 的父级,另一个是轮播 div 本身。

这是我的标记:

<section id="some-id" class="team">
  <div class="wrapper"> <!-- this gets removed on destroy -->
    <header><!-- content --></header>
    <div class="owlCarousel"> <!-- and this gets removed on destroy -->
      <article class="big"><!-- content contains another .wrapper --></article>
      <article class="big"><!-- content contains another .wrapper --></article>
      <article class="small"><!-- content --></article>
      <article class="small"><!-- content --></article>
      <!-- and some more articles -->
    </div>
  </div>
</section>
Run Code Online (Sandbox Code Playgroud)

这是我使用的JS:

var $owlTeam;
if( $window.width() < 680 ) {
    $('.team .owlCarousel').owlCarousel({
        autoPlay: …
Run Code Online (Sandbox Code Playgroud)

javascript jquery owl-carousel

2
推荐指数
1
解决办法
4616
查看次数

标签 统计

javascript ×2

jquery ×2

css ×1

css-grid ×1

owl-carousel ×1