如何添加单击事件以将您带到幻灯片上的某个图像

How can I add a click event to take you to a certain image on a slide

本文关键字:幻灯片 图像 何添加 添加 事件 单击      更新时间:2023-09-26

我有一张幻灯片,里面有一本数码书的图像,分为三章。滑块正在工作,但我如何在html中的"Chapter"div上添加单击事件,以便当我单击它们时,它滚动到滑块中的正确位置。即,如果您点击第2章,它将带您进入第2章开始的幻灯片。这是使用jquery。

HTML

<div class="chapters">
    <div class="chapterOne">
            <h3>Stony Brook Medicine</h3>
            <h4>Chapter 1</h4>
    </div>
    <div class="chapterTwo">
            <h3>Stony Brook Childrens</h3>
            <h4>Chapter 2</h4>  
    </div>
    <div class="chapterThree">
            <h3>Working together while standing out</h3>
            <h4>Chapter 3</h4>  
    </div>
</div>
</div>
<div class="sliderContainer">
    <ul>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/F44D5E"><a class="arrow next">&rang;</a>
        </li>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/56DC28"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/56DC28"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/56DC28"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterOneSlide">
            <img src="http://placehold.it/875x369/56DC28"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterTwoSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterTwoSlide">
            <img src="http://placehold.it/875x369/56DC28"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterTwoSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterTwoSlide">
            <img src="http://placehold.it/875x369/56DC28"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterTwoSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterThreeSlide">
            <img src="http://placehold.it/875x369/56DC28"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterThreeSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterThreeSlide">
            <img src="http://placehold.it/875x369/56DC28"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterThreeSlide">
            <img src="http://placehold.it/875x369/CEBAC9"><a class="arrow next">&rang;</a><a class="arrow prev">&lang;</a>
        </li>
        <li class="chapterThreeSlide">
            <img src="http://placehold.it/875x369/E5A470"><a class="arrow prev">&lang;</a>
        </li>
    </ul>
</div>

和jquery

$(window).load(function () {
    var theImage = $('ul li img');
    var theWidth = theImage.width()
    //wrap into mother div
    $('ul').wrap('<div id="mother" />');
    //assign height width and overflow hidden to mother
    $('#mother').css({
        width: function () {
            return theWidth;
        },
        height: function () {
            return theImage.height();
        },
        position: 'relative',
        overflow: 'hidden'
    });
    //get total of image sizes and set as width for ul
    var totalWidth = theImage.length * theWidth;
    $('ul').css({
        width: function () {
            return totalWidth;
        }
    });

    $(theImage).each(
    function (intIndex) {
        $(this).nextAll('a')
            .bind("click", function () {
            if ($(this).is(".next")) {
                $(this).parent('li').parent('ul').animate({
                    "margin-left": (-(intIndex + 1) * theWidth)
                }, 1000)
            } else if ($(this).is(".prev")) {
                $(this).parent('li').parent('ul').animate({
                    "margin-left": (-(intIndex - 1) * theWidth)
                }, 1000)
            } else if ($(this).is(".startover")) {
                $(this).parent('li').parent('ul').animate({
                    "margin-left": (0)
                }, 1000)
            }
        }); //close .bind()
    }); //close .each()
});

CSS

    .sliderContainer {
    border: 2px solid black;
    width: 875px;
    height: 369px;
    position: relative
}
.sliderContainer:hover .arrow {
    opacity: 1;
    transition: all .5s ease-out;
    -webkit-transition: all .5s ease-out;   
}
.arrow {
    text-align: center;
    line-height: 50px;
    color: #fff;
    width: 50px;
    height: 50px;
    background-color: rgba(183,3,3,0.5);
    cursor: pointer;
    z-index: 99;
    top: 162px;
    position: absolute;
}
.next {
    right: 0;
    opacity: 0;
}
.prev {
    left: 0;
    opacity: 0;
}
ul {
    padding: 0; 
    margin:0;
}
ul li {
  float:left; 
  list-style:none; 
  position:relative; 
}

您也可以只使用CSS来完成JS Fiddle,只需将.chapters内部div更改为a标签,并在每张幻灯片的第一张幻灯片中设置目标a href="#foo" li id="foo",它就会跳到那个章节,并具有轻微的褪色效果

CSS:

 :target {
   animation: slideFadeIn 2s ease;
 }
 @keyframes slideFadeIn {
   0% {
     opacity: 0;
   }
   100% {
     opacity: 1;
   }
 }