为什么类型转盘在Codepen中工作,而在Jsfiddle中不起作用?

时间:2018-06-02 12:10:43

标签: javascript jsfiddle codepen

我根据我在Codepen中的需求定制了这段代码,它就像一个魅力。由于我更喜欢​​使用Jsfiddle,我只是将完全相同的代码(HTML,CSS和JS)复制并粘贴到Jsfiddle界面中,但它不起作用。

这里有效:https://codepen.io/anon/pen/WyQXZz

这里没有:https://jsfiddle.net/chevalier/1cockm25/1/

var TxtRotate = function(el, toRotate, period) {
"use strict";
this.toRotate = toRotate;
  this.el = el;
  this.loopNum = 0;
  this.period = parseInt(period, 10) || 2000;
  this.txt = '';
  this.tick();
  this.isDeleting = true;
};

TxtRotate.prototype.tick = function() {
    "use strict";
  var i = this.loopNum % this.toRotate.length;
  var fullTxt = this.toRotate[i];

  if (this.isDeleting) {
    this.txt = fullTxt.substring(0, this.txt.length - 1);
  } else {
    this.txt = fullTxt.substring(0, this.txt.length + 1);
  }

  this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>';

  var that = this;
  var delta = 150 - Math.random() * 100;

  if (this.isDeleting) { delta /= 2; }

  if (!this.isDeleting && this.txt === fullTxt) {
    delta = this.period;
    this.isDeleting = true;
  } else if (this.isDeleting && this.txt === '') {
    this.isDeleting = false;
    this.loopNum++;
    delta = 500;
  }

  setTimeout(function() {
    that.tick();
  }, delta);
};

window.onload = function() {
    "use strict";
  var elements = document.getElementsByClassName('txt-rotate');
  for (var i=0; i<elements.length; i++) {
    var toRotate = elements[i].getAttribute('data-rotate');
    var period = elements[i].getAttribute('data-period');
    if (toRotate) {
      new TxtRotate(elements[i], JSON.parse(toRotate), period);
    }
  }
  // INJECT CSS
  var css = document.createElement("style");
  css.type = "text/css";
  css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid white }";
  document.body.appendChild(css);
};

在我的网站上,它也不起作用。我不熟悉JS,所以我需要帮助解决这个问题。

谢谢!

0 个答案:

没有答案