コード
HTML
<div id="slide">
<ul>
<li> <img src="../../images/news/slideshow/slide_image01.png" alt=""></li>
<li> <img src="../../images/news/slideshow/slide_image02.png" alt=""></li>
<li><img src="../../images/news/slideshow/slide_image03.png" alt=""></li>
<li> <img src="../../images/news/slideshow/slide_image04.png" alt=""></li>
<li><img src="../../images/news/slideshow/slide_image05.png" alt=""></li>
<li><img src="../../images/news/slideshow/slide_image06.png" alt=""></li>
</ul>
<button id="prevBtn" class="slide_btn"><</button>
<button id="nextBtn" class="slide_btn">></button>
<!--/.slide--></div>
CSS
@media only screen and (min-width:767px), print {
#slide {
position: relative;
overflow: hidden;
min-height: 432px;
}
#slide ul {
width: 4608px;
margin: 0;
padding: 0;
position: absolute;
top: 0;
left: 0;
}
#slide li {
width: 768px;
height:auto;
}
#slide img {
display: block;
width:100%;
min-width: 768px;
height: auto;
min-height:432px;
aspect-ratio:16/9;
}
.slide_btn {
width: 50px;
height: 100px;
background: #ccc;
color: #fff;
font-size: 2.4rem;
}
.slide_btn:hover {
opacity: 0.6;
}
#prevBtn {
position: absolute;
top: 166px;
left: 0;
}
#nextBtn {
position: absolute;
top: 166px;
left: 684px;
}
}
@media only screen and (min-width:1024px), print {
#slide {
position: relative;
overflow: hidden;
min-height: 402px;
}
#slide ul {
width: 4284px;
margin: 0;
padding: 0;
position: absolute;
top: 0;
left: 0;
}
#slide li {
width: 714px;
height: auto;
}
#slide img {
display: block;
width: 100%;
min-width: 714px;
height: auto;
min-height:402px;
aspect-ratio:16/9;
}
.slide_btn {
width: 50px;
height: 100px;
background: #ccc;
color: #fff;
font-size: 2.4rem;
opacity: 0.8;
transition: 0.3s;
}
.slide_btn:hover {
background: #fff;
color: #ccc;
}
#prevBtn {
position: absolute;
top: 166px;
left: 0;
}
#nextBtn {
position: absolute;
top: 166px;
left: 664px;
}
}
JS
$(function(){
//スクロールの方向
var dir = -1;
//スクロールのインターバル
var interval = 3000;
//スクロールのスピード
var duration = 700;
//タイマー用の変数
var timer;
//リストの順番を変更
$("#slide ul").prepend($("#slide li:last-child"));
$("#slide ul").css("left",-714);
timer = setInterval(slideTimer,interval);
function slideTimer(){
if(dir == -1){
$("#slide ul").animate({"left":"-=714px"},duration,
function(){
$(this).append($("#slide li:first-child"));
$(this).css("left",-714);
});
}else{
$("#slide ul").animate({"left":"+=714px"},duration,
function(){
$(this).prepend($("#slide li:last-child"));
$(this).css("left",-714);
dir = -1;
});
}
}/*slideTimer*/
//前へ戻るボタン
$("#prevBtn").click(function(){
dir = 1;
clearInterval(timer);
timer = setInterval(slideTimer,interval);
slideTimer();
});
//次へ進むボタン
$("#nextBtn").click(function(){
dir = -1;
clearInterval(timer);
timer = setInterval(slideTimer,interval);
slideTimer();
});
});