npm 可以将开源的
node.js 项目发布到在线仓库,当然我们也可以使用
npm 命令下载项目中所需要的
package 。我们可以在
https://www.npmjs.com/ 网站上搜索我们所需要的
package 。所以首先点击链接打开该网站,在其中搜索我们需要的
package 名字,比如在该篇文章中使用的
swiper 。
运行效果
如果觉得步骤太长,想直接使用该代码,可以直接跳转到第10步,复制代码到html 文件中直接运行,但要先把图片路径替换为自己的路径。
使用步骤
1. 打开 https://www.npmjs.com,在搜索框中输入swiper ,回车; ??????
2. 点击打开swiper 的文档说明,点击 Getting Started Guide ??????
3. 新建一个文件夹,比如swiper-demo ,用vscode 打开该文件夹,初始化node.js 项目,然后在终端中输入npm i swiper 下载依赖。(如果不了解怎么创建一个node.js 项目,可以点击这里参考)
4. 新建src 文件夹,在其中新建index.html 文件 5. 打开swiper文档,根据文档说明将swiper 依赖以及样式导入到index.html 中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用swiper</title>
<link rel="stylesheet" href="../node_modules/swiper/swiper-bundle.css">
</head>
<body>
<script src="../node_modules/swiper/swiper-bundle.js"></script>
</body>
</html>
6. 将基本的Swiper 布局复制粘贴到index.html 文件中
<!DOCTYPE html>
<html lang="en">
<head>...</head>
<body>
<div class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-scrollbar"></div>
</div>
<script src="../node_modules/swiper/swiper-bundle.js"></script>
</body>
</html>
7. 设置Swiper 的大小
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用swiper</title>
<link rel="stylesheet" href="../node_modules/swiper/swiper-bundle.css">
<style>
.swiper {
width: 600px;
height: 300px;
}
</style>
</head>
<body>...</body>
</html>
8. 最后需要初始化Swiper ,全部代码如下。(如果不想使用npm 下载依赖的话,可以将导入Swiper 依赖和导入swiper 样式的代码替换为下面两行)
<link rel="stylesheet" href="https://unpkg.com/swiper@7/swiper-bundle.min.css" />
<script src="https://unpkg.com/swiper@7/swiper-bundle.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用swiper</title>
<link rel="stylesheet" href="../node_modules/swiper/swiper-bundle.css">
<style>
.swiper {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-scrollbar"></div>
</div>
<script src="../node_modules/swiper/swiper-bundle.js"></script>
<script>
const swiper = new Swiper('.swiper', {
direction: 'vertical',
loop: true,
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
scrollbar: {
el: '.swiper-scrollbar',
}
})
</script>
</body>
</html>
9. 运行效果如下
10. 我们可以按照需要来修改,让其呈现的效果更美观,代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用swiper</title>
<link rel="stylesheet" href="https://unpkg.com/swiper@7/swiper-bundle.min.css" />
<style>
.swiper {
width: 600px;
height: 300px;
}
img {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div class="swiper" onmouseover="play(false)" onmouseleave="play(true)">
<div class="swiper-wrapper">
<div class="swiper-slide">
<img src="./assets/bg01.jpg">
</div>
<div class="swiper-slide">
<img src="./assets/bg02.jpg">
</div>
<div class="swiper-slide">
<img src="./assets/bg03.jpg">
</div>
<div class="swiper-slide">
<img src="./assets/bg04.jpg">
</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
<script src="https://unpkg.com/swiper@7/swiper-bundle.min.js"></script>
<script>
const swiper = new Swiper('.swiper', {
autoplay: {
delay: 1000,
},
direction: 'horizontal',
loop: true,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
}
})
function play(flag) {
console.log('鼠标~~~', flag);
if (flag) {
swiper.autoplay.start()
} else {
swiper.autoplay.stop()
}
}
</script>
</body>
</html>
参考链接
Getting Started With Swiper 什么是node.js和npm node.js的下载与安装
|