javascript - Initialize Slider in materialize css
问题描述
I copied the example code for the materialize slider and pasted it into my html. I linked the html with a JavaScript file, where I placed an
M.AutoInit();
Which should initialize all JS components of materialize.css
(Carousel works in my html)
This is the related html:
<div class="row ">
<div class="col s12 m12 ">
<div class="slider">
<ul class="slides">
<li>
<img src="https://lorempixel.com/580/250/nature/4">
<!-- random image -->
<div class="caption center-align">
<h3>This is our big Tagline!</h3>
<h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
</div>
</li>
</ul>
</div>
</div>
</div>
And this is how I linked to the JavaScript: First materialize.js
that app.js
where I M.AutoInit();
<script type="text/JavaScript" src="../js/materialize.js"></script>
<!-- <script type="text/JavaScript" src="../js/materialize.min.js"></script> -->
<script src="../app.js "></script>
</html>
I am confused, because the carousel works within the same setting but not the slider…</p>
解决方案
2 things to change in your code:
- Make sure your initialization code is executed just after the DOM Content is totally loaded
- Use the specific initializator of the slider instead of the generic one
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.slider');
var instances = M.Slider.init(elems);
});
Working example with your code: https://codepen.io/jhervas/pen/mddNrZX
推荐阅读
- javascript - 为什么在 JavaScript 中显示数组元素的上限为 100,有没有办法在没有循环或 splice() 的情况下显示所有内容?
- python - 在 Python3 类型中创建无硬编码的 Union 类型
- django - 哪个框架最适合规格表和技术规格表程序?
- javascript - 如何使用 JS 将 HTML 标签格式转换为 Xml
- javascript - `file_get_contents("php://input")` 是从 `fetch` 获取帖子数据的唯一方法吗?使用异步和 hack 方式是否安全?
- css - 如何将元素相对于其他元素居中,但所有元素都适合使用 CSS 的祖先?
- python - Python3 ModuleNotFoundError 导入
- typescript - “Vue”类型上不存在属性“$keycloak”
- javascript - javascript。ajax调用后如何传递“this”对象?
- php - Prestashop 1.7,显示组合产品的最低价格