Flickity & Swup - 破坏弹力

问题描述

我在使用 Swup 进行页面转换时试图销毁并重新加载我的 Flickity 幻灯片,但我运气不佳。这是我的 js 文件

import Swup from 'swup';

var Flickity = require('flickity');

function init() {
  if (document.querySelector('.testimonials-slideshow')) {
    var flkty = new Flickity('.testimonials-slideshow',{
      wrapAround: true,pageDots: false,autoplay: true,arrowShape: 'M68.374,83.866L31.902,50L68.374,16.134L64.814,12.3L24.214,50L64.814,87.7L68.374,83.866Z'
    });
  }
}

function unload() {
  flkty.destroy();
}

init();

const swup = new Swup();

swup.on('contentReplaced',init);

swup.on('willReplaceContent',unload);

但是当我尝试这样做时,我收到错误 flkty is not defined。任何人都可以给我任何指示吗?

解决方法

变量范围

正如 CBroe 所提到的,您的 var 是未定义的,因为您定义了它。它在函数中定义,但应在“顶层”定义。

import Swup from 'swup';

var Flickity = require('flickity');

// Added a "global" definition here:
var flkty;

function init() {
  if (document.querySelector('.testimonials-slideshow')) {
    // Removed var:
    flkty = new Flickity('.testimonials-slideshow',{
      wrapAround: true,pageDots: false,autoPlay: true,arrowShape: 'M68.374,83.866L31.902,50L68.374,16.134L64.814,12.3L24.214,50L64.814,87.7L68.374,83.866Z'
    });
  }
}

function unload() {
  flkty.destroy();
}

init();

const swup = new Swup();

swup.on('contentReplaced',init);

swup.on('willReplaceContent',unload);

此外,如果您正在使用任何类型的模块捆绑器,有时它仍然会丢失,因此您可以考虑执行以下操作:

window.flkty = new Flickity('.testimonials-slideshow',...

并始终以这种方式引用它,即

window.flkty.destroy();

只销毁存在的实例

这就是你的变量定义。下一个潜在错误是您仅在查询选择器匹配时才初始化 flkty

if (document.querySelector('.testimonials-slideshow')) {

但是你每 willReplaceContent 销毁它,所以你真的可以检查“它是否初始化,这个页面加载?”。在这种情况下,您可以像这样进行检查:

// Init the var as false:
var flkty = false

function init() {
  if (document.querySelector('.testimonials-slideshow')) {
    flkty = new Flickity('.testimonials-slideshow',...);
  }
}

function unload() {
  if(flkty){
    flkty.destroy();
    // Make sure the flkty var is set to false at the end:
    flkty = false;
  }
}

整理你的代码

这一切都会有点失控,所以我们开始做的是创建模块。这是我们使用的 carousel 模块的骨架:

// modules/Carousel.js
import Swiper from "swiper";

export default {
  carouselEl: null,carouselSwiper: null,setup() {
    this.carouselEl = document.getElementById("header-carousel");
    if (!this.carouselEl) {
      // Just stop if there is no carousel on this page
      return;
    }
    this.carouselSwiper = new Swiper(this.carouselEl,{ ... });
    this.carouselSwiper.on("slideChange",() => { ... });
  },destroy() {
    // If we already have one:
    if (this.carouselSwiper) {
      this.carouselSwiper.destroy();
    }
    // Make sure we are reset,ready for next time:
    this.carouselSwiper = null;
  },};

然后,在我们的 main.js 中,我们会做一些与您类似的事情:

import Carousel from "./modules/Carousel.js";

function init(){
  Carousel.setup();
  // Add more here as the project grows...
}
function unload(){
  Carousel.unload();
}

swup = new Swup();
swup.on("contentReplaced",init);
swup.on("willReplaceContent",unload);
init();

所有模块都有 setupunload 函数,如果元素不存在,它们不会中断,因此我们可以在每个页面加载和卸载时调用它们。

我喜欢 swup,但在启动和破坏事物的噩梦中也有亲身经历,所以如果您需要任何进一步的帮助,请告诉我。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...