首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换html后重新加载flickity

替换html后重新加载flickity
EN

Stack Overflow用户
提问于 2020-03-31 18:43:20
回答 1查看 986关注 0票数 0

我运行多个flickity,它在第一个html代码中正确运行。虽然在点击按钮后用新的html .boxgallery替换.boxgallery是不正确的运行flickity。

我做什么好?(未更改的replaceWith js)

代码语言:javascript
复制
var $carousel = $('.gallery').flickity();
var isFlickity = true;
$('.button').on( 'click', function()
{
	//Clearing previous contents
	$carousel.flickity('destroy');
  
	$('.boxgallery').replaceWith('<div class="boxgallery"><div class="gallery"><div class="gallery-cell"></div><div class="gallery-cell"></div><div class="gallery-cell"></div></div></div>');

	// init new Flickity
	$carousel.flickity();
});
代码语言:javascript
复制
@import url(http://flickity.metafizzy.co/flickity.css);
* {
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
}

.gallery {
    width: 200px;
    background: #FAFAFA;
    margin-bottom: 30px;
    counter-reset: gallery-cell;
    float: right;
    margin: 10px;
}

.gallery-cell {
  width: 100%;
  height: 160px;
  margin-right: 20px;
  background: #8C8;
  counter-increment: gallery-cell;
}

.gallery-cell:before {
  content: counter(gallery-cell);
  display: block;
  text-align: center;
  line-height: 160px;
  font-size: 80px;
  font-weight: bold;
  color: white;
}

.gallery-nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

.gallery-nav li {
  display: inline-block;
  font-size: 20px;
  color: #09F;
  border: 1px solid;
  padding: 10px;
  margin-right: 5px;
}

.gallery-nav li.is-selected {
  background: #09F;
  color: white;
}

.gallery-nav li:hover {
  background: #ADF;
  cursor: pointer;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flickity/2.0.5/flickity.pkgd.min.js" type="text/javascript" charset="utf-8"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flickity/2.0.5/flickity.min.css">

<button class="button">Re Create Flickity</button>

<div class="boxgallery">
  <div class="gallery">
    <div class="gallery-cell"></div>
    <div class="gallery-cell"></div>
    <div class="gallery-cell"></div>
  </div>

  <div class="gallery">
    <div class="gallery-cell"></div>
    <div class="gallery-cell"></div>
  </div>

  <div class="gallery">
    <div class="gallery-cell"></div>
    <div class="gallery-cell"></div>
    <div class="gallery-cell"></div>
    <div class="gallery-cell"></div>
  </div>
</div>

演示:http://jsfiddle.net/74bcqf6x/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-01 00:59:12

请参阅打开和关闭Flickity的destroy演示,但我认为使用resize会更好

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60948712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档