javascript - 在 Yii2 上使用 lib CountUp.js
问题描述
如何在 Yii2 中正确使用 CountUp.js 库?我已经添加了它AppAsset
并在 View 中正确加载,现在我将分配 lib 的使用和在同一视图中显示的数字
应用资产.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
'css/custom-bootstrap.css',
'css/font-awesome-4.7.0/css/font-awesome.css',
];
public $js = [
'js/countup.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
有人可以给我一个在视图中使用的例子吗?
解决方案
如果您使用的是库的 javascript 版本,则countUp.js
需要CountUp(target, startVal, endVal, decimals, duration, options)
使用配置参数创建一个新实例,然后调用instance.start();
以启动计数器。
参数:
target
= html 元素、输入、svg 文本元素的 id 或先前选择的元素/输入的 var,其中发生计数startVal
=您要开始的值endVal
=您想要达到的值decimals
=(可选)数字中的小数位数,默认为 0duration
=(可选)持续时间,以秒为单位,默认为 2options
=(可选,见演示)格式化/缓动选项对象
我假设您已经注册了上面的AppAsset
类并加载了源库,否则取消注释countUp.js
下面视图顶部的 CDN 链接,复制以下视图并运行它
<?php
use yii\web\View;
//$this->registerJsFile('//cdn.jsdelivr.net/npm/countup@1.8.2/dist/countUp.min.js');
$js = <<<JS
var options = {
useEasing: true,
useGrouping: true,
separator: ',',
decimal: '.',
};
var demo = new CountUp('counter', 0, 5220, 0, 2.5, options);
if (!demo.error) {
demo.start();
} else {
console.error(demo.error);
}
JS;
$this->registerJs($js, View::POS_END);
?>
<div id="counter">
</div>
推荐阅读
- asp.net-core - 从 aspnet 核心应用程序到 cloudwatch 的详细应用程序日志
- javascript - 第三方脚本设置第一方 cookie
- sql - T-SQL WHERE NOT EXISTS - 结果不一致
- java - 我可以使用默认的 TestNG 线程框架在循环内进行线程处理吗?
- java - 我想读取一个数组,但是当我按下播放按钮时没有任何反应!我的代码有什么问题?
- python - 两个数据框之间的区别
- angular - Angular2将十六进制转换为文件并显示它
- typescript - Vuex - 模型嵌套模块的依赖
- passport.js - 带护照 JS 的 GCF
- javascript - jQuery/CSS 仅在具有特定背景图像时才将鼠标悬停在按钮上