圖片輪播(含左右按鈕切換,底部圓點切換,滑鼠滑動切換)
阿新 • • 發佈:2022-05-23
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title></title> <!-- 最新版本的 Bootstrap 核心 CSS 檔案 --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"> <!-- jquery --> <script src="https://fastly.jsdelivr.net/npm/[email protected]/dist/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"crossorigin="anonymous"></script> <!-- 最新的 Bootstrap 核心 JavaScript 檔案 --> <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script> <link rel="stylesheet" href="./css/index.css"> </head> <body> <!-- bootstrap官方輪播圖程式碼 --> <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> <li data-target="#carousel-example-generic" data-slide-to="1"></li> <li data-target="#carousel-example-generic" data-slide-to="2"></li> </ol> <div class="carousel-inner" role="listbox"> <!-- 圖片地址改成你自己的就可以了 --> <div class="item active"> <img src="__APP__/img/index/資源 1.png" alt="..."> </div> <div class="item"> <img src="__APP__/img/index/資源 1.png" alt="..."> </div> <div class="item"> <img src="__APP__/img/index/資源 1.png" alt="..."> </div> </div> <!-- 給兩側切換輪播圖的a標籤加了id --> <a id="leftbtn" class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a id="rightbtn" class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> <!-- hammer.js --> <script src="http://hammerjs.github.io/dist/hammer.min.js"></script> <script type="text/javascript"> // 建立一個新的hammer物件並且在初始化時指定要處理的dom元素 // 通過id獲取到裝輪播圖的容器 var hammertime = new Hammer(document.getElementById("carousel-example-generic")); //新增左滑動事件 hammertime.on("swipeleft", function (e) { // 自動觸發左側按鈕 document.getElementById('leftbtn').click(); }) //新增右滑動事件 hammertime.on("swiperight", function (e) { // 自動觸發右側按鈕 document.getElementById('rightbtn').click(); }) </script> </body> </html>