javascript - 如何使用 leader-line js 在 Swiper 幻灯片之间制作动画线条?
问题描述
我正在尝试使用 Swiper 和 leader-line.js 构建时间线。我尝试将它与 Swiper 的转换功能一起使用,但它不能正常工作。这是我的领导行代码;
const items = document.querySelectorAll(".circle");
const options = {
startSockets: ["top", "bottom", "right", "bottom", "left", "right"],
endSockets: ["top", "bottom", "top", "top", "top", "top"]
};
for(let i = 0; i < items.length - 1; i++) {
new LeaderLine(items[i], items[i + 1], {
startPlug: "behind",
endPlug: "behind",
size: 2,
dash: true,
startPlugSize: 3,
endPlugSize: 1,
startSocket: "right",
endSocket: "left",
color: "#fb8c00",
path: 'straight',
});
}
我做了一个codepen例子来说明清楚。
https://codepen.io/Xteripus/pen/yLopBNp?editors=1010
我想让线条粘在 Swiper 的容器上,所以当 swiper 元素移动时,线条会随之移动。提前致谢!
解决方案
插件开发者 Anseki 提出了这个解决方案,希望对您有所帮助!
这是js:
$(".circle").each(function (index, el) {
$(el).css({
top: Math.random() * ($(".swiper-slide ").height() - 300 - $(this).height())
});
});
const items = document.querySelectorAll(".circle");
const options = {
startSockets: ["top", "bottom", "right", "bottom", "left", "right"],
endSockets: ["top", "bottom", "top", "top", "top", "top"]
};
const lines = [];
for (let i = 0; i < items.length - 1; i++) {
lines.push(
new LeaderLine(items[i], items[i + 1], {
startPlug: "behind",
endPlug: "behind",
size: 2,
dash: true,
startPlugSize: 3,
endPlugSize: 1,
startSocket: "right",
endSocket: "left",
color: "#fb8c00",
path: "straight"
})
);
}
swiper.on("sliderMove", () => {
lines.forEach((line) => {
line.position();
});
});
let req;
function position(stop) {
cancelAnimationFrame(req);
lines.forEach((line) => {
line.position();
});
if (stop !== true) {
req = requestAnimationFrame(position);
}
}
swiper.on("slideChangeTransitionStart", position);
swiper.on("slideResetTransitionStart", position);
swiper.on("slideChangeTransitionEnd", () => {
position(true);
});
swiper.on("slideResetTransitionEnd", () => {
position(true);
});
推荐阅读
- java - 有没有办法从 JAVA 的浏览器中获取所有打开的 URL?
- docker - Spark on Kubernetes : /opt/entrypoint.sh: /sbin/tini: No such file or directory 错误
- python-3.x - Python中Selenium webdriver中元素的条件选择
- java - 如果我运行我的数组程序,0 会显示我如何删除它?
- google-data-studio - 我可以使用数据工作室 API 从报告中提取数据吗?
- php - 使用 PHP 在 FPDF 中的单元格中自动换行?
- swift - snapkit 登录工具包不工作 Swift Xcode 11.3.1
- java - 在获取特定数据集的 BQ 表列表时获取 AppIdentityServiceFailureException getAccessToken
- reactjs - 在反应中获取 url 作为对象
- python - Chart.js 不显示 Django Rest API 数据