如何从客户端(Javascript,Node)使用spin.js

yko*_*nda 0 javascript node.js npm spin.js

我正在尝试从客户端使用spin.js(https://spin.js.org/#!),但是我有一些问题。
在做

npm install spin.js
Run Code Online (Sandbox Code Playgroud)

然后

const Spinner = require('spin.js');
Run Code Online (Sandbox Code Playgroud)

无法使用,因为您需要浏览该模块才能使用客户端中的模块。

我也尝试过复制并过去spin.js(https://spin.js.org/spin.js)并从html引用它,但是它给了我spin.js错误

export { Spinner };
Run Code Online (Sandbox Code Playgroud)

Uncaught SyntaxError: Unexpected token export
Run Code Online (Sandbox Code Playgroud)

从客户端使用spin.js需要什么?

yko*_*nda 5

好的,所以我知道了。

您要做的是:

将spin.js文件复制到本地文件系统(称为spin.js),并在html文件中body标记的末尾引用本地spin.js。

...
<script type="text/javascript" src="js/spin.js"></script>
<script type="text/javascript" src="js/scriptWithSpinner.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)

在spin.js中注释掉以下行。

export { Spinner };
Run Code Online (Sandbox Code Playgroud)

https://spin.js.org/spin.css复制CSS 并将其存储在本地文件系统中。请参阅html文件标题中的CSS。

<head>
    <meta charset="utf-8">
    ...

    <link rel="stylesheet" type="text/css" href="mystyles.css" />
    <link rel="stylesheet" type="text/css" href="spin.css" />
    ...
</head>
Run Code Online (Sandbox Code Playgroud)

如果使用的是express,则可能需要在服务器代码中使用js和CSS公开目录,以便html文件可以读取它。

现在,您可以直接从scriptWithSpinner.js使用全局定义的Spinner对象,无需导入或不需要。

scriptWithSpinner.js

var opts = {
  lines: 13, // The number of lines to draw
  length: 38, // The length of each line
  width: 17, // The line thickness
  radius: 45, // The radius of the inner circle
  scale: 1, // Scales overall size of the spinner
  corners: 1, // Corner roundness (0..1)
  color: '#ffffff', // CSS color or array of colors
  fadeColor: 'transparent', // CSS color or array of colors
  speed: 1, // Rounds per second
  rotate: 0, // The rotation offset
  animation: 'spinner-line-fade-quick', // The CSS animation name for the lines
  direction: 1, // 1: clockwise, -1: counterclockwise
  zIndex: 2e9, // The z-index (defaults to 2000000000)
  className: 'spinner', // The CSS class to assign to the spinner
  top: '50%', // Top position relative to parent
  left: '50%', // Left position relative to parent
  shadow: '0 0 1px transparent', // Box-shadow for the lines
  position: 'absolute' // Element positioning
};

var target = document.getElementsByClassName('uploader')[0];
var spinner = new Spinner(opts).spin(target);
Run Code Online (Sandbox Code Playgroud)