如何在我的轮播上产生这种悬停效果?

基本上,我制作了一个 3D 轮播,其中包含 3 个图像。当用户单击“下一个”和“上一个”按钮时,轮播会前后移动。我还在顶部有一个单独的图像来产生这种悬停效果。我想将悬停效果添加到轮播中的图像上,具有相同的动画、不透明度、文本、大小等。但是,我不知道如何包含它。我怎样才能做到这一点?任何帮助,将不胜感激。这是我的代码。谢谢。


var carousel = $(".carousel"),

    currdeg  = 0;


$(".next").on("click", { d: "n" }, rotate);

$(".prev").on("click", { d: "p" }, rotate);


function rotate(e){

  if(e.data.d=="n"){

    currdeg = currdeg - 120;

  }

  if(e.data.d=="p"){

    currdeg = currdeg + 120;

  }

  carousel.css({

    "-webkit-transform": "rotateY("+currdeg+"deg)",

    "-moz-transform": "rotateY("+currdeg+"deg)",

    "-o-transform": "rotateY("+currdeg+"deg)",

    "transform": "rotateY("+currdeg+"deg)"

  });

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!DOCTYPE html>

<html>

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link href="style.css" rel="stylesheet" type="text/css">

  <title>Document</title>

</head>

<body>

   <div class="content">

   <a href="#open-modal">

     <div class="content-overlay"></div>

     <img class="content-image" src="https://static.independent.co.uk/s3fs-public/thumbnails/image/2014/03/25/12/eiffel.jpg">

     <div class="content-details fadeIn-bottom">

       <h3 class="content-title">Eiffel Tower</h3>

     </div>

   </a>

 </div>

  <div class="container">

  <div class="carousel">

    <div class="item a"><img src="https://www.insidehook.com/wp-content/uploads/2019/08/HERO-3.jpg?fit=1200%2C750" width="250px" height="200px"></div>

    <div class="item b"><img src="https://static.independent.co.uk/s3fs-public/thumbnails/image/2014/03/25/12/eiffel.jpg" width="250px" height="200px"></div>

    <div class="item c"><img src="https://nypost.com/wp-content/uploads/sites/2/2019/12/robberies-serious-crimes-spike-in-central-park-this-year.jpg?quality=80&strip=all" width="250px" height="200px"></div>

  </div>

</div>

<div class="next">Next</div>

<div class="prev">Prev</div>


  <script src="script.js"></script>

</body>

</html>


吃鸡游戏
浏览 93回答 1
1回答

拉莫斯之舞

对于解决方案,我必须对 css、html 进行一些更改并修复 js 代码以确定活动幻灯片。这样的结果有必要吗?var carousel = $(".carousel"),&nbsp; &nbsp; currdeg = 0,&nbsp; &nbsp; current = 0;$(".next").on("click", {d: "n"}, rotate);$(".prev").on("click", {d: "p"}, rotate);function rotate(e) {&nbsp; &nbsp; $(carousel.find('.active')).removeClass('active');&nbsp; &nbsp; if (e.data.d == "n") {&nbsp; &nbsp; &nbsp; &nbsp; currdeg = currdeg - 120;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; current = (current + 1) % carousel.find('.item').length;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (e.data.d == "p") {&nbsp; &nbsp; &nbsp; &nbsp; currdeg = currdeg + 120;&nbsp; &nbsp; &nbsp; &nbsp; current = (current - 1 + carousel.find('.item').length) % carousel.find('.item').length;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $(carousel.find('.item')[current]).addClass('active');&nbsp; &nbsp; carousel.css({&nbsp; &nbsp; &nbsp; "-webkit-transform": "rotateY(" + currdeg + "deg)",&nbsp; &nbsp; &nbsp; "-moz-transform": "rotateY(" + currdeg + "deg)",&nbsp; &nbsp; &nbsp; "-o-transform": "rotateY(" + currdeg + "deg)",&nbsp; &nbsp; &nbsp; "transform": "rotateY(" + currdeg + "deg)"&nbsp; &nbsp; });&nbsp;}.container {&nbsp; margin: -100px auto;&nbsp; width: 250px;&nbsp; height: 200px;&nbsp; position: relative;&nbsp; top: 500px;&nbsp; perspective: 1000px;&nbsp;&nbsp;}.carousel {&nbsp; height: 100%;&nbsp; width: 100%;&nbsp; position: absolute;&nbsp; transform-style: preserve-3d;&nbsp; transition: transform 1s;}.item {&nbsp; display: block;&nbsp; position: absolute;&nbsp; background: #000;&nbsp; width: 250px;&nbsp; height: 200px;&nbsp; line-height: 200px;&nbsp; font-size: 5em;&nbsp; text-align: center;&nbsp; color: #FFF;&nbsp; /*opacity: 0.95;*/&nbsp;&nbsp;}.item.active {&nbsp; cursor: pointer;&nbsp;&nbsp;}.a {&nbsp; transform: rotateY(0deg) translateZ(250px);}.b {&nbsp; transform: rotateY(120deg) translateZ(250px);}.c {&nbsp; transform: rotateY(240deg) translateZ(250px);}.next, .prev {&nbsp; color: #444;&nbsp; position: absolute;&nbsp; top: 500px;&nbsp; padding: 1em 2em;&nbsp; cursor: pointer;&nbsp; background: #CCC;&nbsp; border-radius: 5px;&nbsp; border-top: 1px solid #FFF;&nbsp; box-shadow: 0 5px 0 #999;&nbsp; transition: box-shadow 0.1s, top 0.1s;}.next:hover, .prev:hover { color: #000; }.next:active, .prev:active {&nbsp; top: 504px;&nbsp; box-shadow: 0 1px 0 #999;}.next { right: 5em; }.prev { left: 5em; }.content {&nbsp;position: relative;&nbsp;width: 100%;&nbsp;margin-left: auto;&nbsp;margin-right: auto;&nbsp;max-width: 400px;}.container .title{&nbsp;color: #1a1a1a;&nbsp;text-align: center;&nbsp;margin-bottom: 10px;}&nbsp;.carousel .content-overlay,&nbsp;.content .content-overlay {&nbsp;background: rgba(0,0,0,0.7);&nbsp;position: absolute;&nbsp;height: 100%;&nbsp;width: 100%;&nbsp;opacity: 0;&nbsp;-webkit-transition: all 0.4s ease-in-out 0s;&nbsp;-moz-transition: all 0.4s ease-in-out 0s;&nbsp;transition: all 0.4s ease-in-out 0s;}.content:hover .content-overlay{&nbsp;opacity: 1;}.carousel .item.active:hover .content-overlay{&nbsp;opacity: 1;}&nbsp;.content-image{&nbsp;width: 100%;}&nbsp;.content-details {&nbsp;position: absolute;&nbsp;text-align: center;&nbsp;padding-left: 1em;&nbsp;padding-right: 1em;&nbsp;width: 100%;&nbsp;top: 50%;&nbsp;left: 50%;&nbsp;opacity: 0;&nbsp;-webkit-transform: translate(-50%, -50%);&nbsp;-moz-transform: translate(-50%, -50%);&nbsp;transform: translate(-50%, -50%);&nbsp;-webkit-transition: all 0.3s ease-in-out 0s;&nbsp;-moz-transition: all 0.3s ease-in-out 0s;&nbsp;transition: all 0.3s ease-in-out 0s;}&nbsp;.content:hover .content-details{&nbsp;top: 50%;&nbsp;left: 50%;&nbsp;opacity: 1;}&nbsp;.content-details h3{&nbsp;color: #fff;&nbsp;font-weight: 500;&nbsp;letter-spacing: 0.15em;&nbsp;margin-bottom: 0.5em;&nbsp;text-transform: uppercase;}&nbsp;.content-details p{&nbsp;color: #fff;&nbsp;font-size: 0.8em;}&nbsp;.fadeIn-bottom{&nbsp;top: 80%;}.content {&nbsp;position: relative;&nbsp;width: 100%;&nbsp;margin-left: auto;&nbsp;margin-right: auto;&nbsp;max-width: 400px;}&nbsp;.content .content-overlay {&nbsp;background: rgba(0,0,0,0.7);&nbsp;position: absolute;&nbsp;height: 99%;&nbsp;width: 100%;&nbsp;opacity: 0;&nbsp;-webkit-transition: all 0.4s ease-in-out 0s;&nbsp;-moz-transition: all 0.4s ease-in-out 0s;&nbsp;transition: all 0.4s ease-in-out 0s;}&nbsp;.content:hover .content-overlay{&nbsp;opacity: 1;}&nbsp;.content-image{&nbsp;width: 100%;}&nbsp;.content-details {&nbsp;position: absolute;&nbsp;text-align: center;&nbsp;padding-left: 1em;&nbsp;padding-right: 1em;&nbsp;width: 100%;&nbsp;top: 50%;&nbsp;left: 50%;&nbsp;opacity: 0;&nbsp;-webkit-transform: translate(-50%, -50%);&nbsp;-moz-transform: translate(-50%, -50%);&nbsp;transform: translate(-50%, -50%);&nbsp;-webkit-transition: all 0.3s ease-in-out 0s;&nbsp;-moz-transition: all 0.3s ease-in-out 0s;&nbsp;transition: all 0.3s ease-in-out 0s;}&nbsp;.content:hover .content-details{&nbsp;top: 50%;&nbsp;left: 50%;&nbsp;opacity: 1;}&nbsp;.content-details h3{&nbsp;color: #fff;&nbsp;font-weight: 500;&nbsp;letter-spacing: 0.15em;&nbsp;margin-bottom: 0.5em;&nbsp;text-transform: uppercase;}&nbsp;.content-details p{&nbsp;color: #fff;&nbsp;font-size: 0.8em;}&nbsp;.fadeIn-bottom{&nbsp;top: 80%;}.carousel_content-details {&nbsp;position: absolute;&nbsp;text-align: center;&nbsp;width: 100%;&nbsp;top: 50%;&nbsp;left: 50%;&nbsp;opacity: 0;&nbsp; margin: 0;&nbsp;padding: 0;&nbsp;-webkit-transform: translate(-50%, -50%);&nbsp;-moz-transform: translate(-50%, -50%);&nbsp;transform: translate(-50%, -50%);&nbsp;-webkit-transition: all 0.3s ease-in-out 0s;&nbsp;-moz-transition: all 0.3s ease-in-out 0s;&nbsp;transition: all 0.3s ease-in-out 0s;}&nbsp;.carousel .item.active:hover .carousel_content-details{&nbsp;top: 50%;&nbsp;left: 50%;&nbsp;opacity: 1;}&nbsp;.carousel_content-details h3{&nbsp;color: #fff;&nbsp;font-size: 24px;&nbsp;font-weight: 500;&nbsp;letter-spacing: 0.15em;&nbsp;margin-bottom: 0.5em;&nbsp;text-transform: uppercase;&nbsp;margin: 0;&nbsp;padding: 0;}&nbsp;.carousel_fadeIn-bottom{&nbsp; top: 50%;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><!DOCTYPE html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; <link href="style.css" rel="stylesheet" type="text/css">&nbsp; <title>Document</title></head><body>&nbsp; &nbsp;<div class="content">&nbsp; &nbsp;<a href="#open-modal">&nbsp; &nbsp; &nbsp;<div class="content-overlay"></div>&nbsp; &nbsp; &nbsp;<img class="content-image" src="https://static.independent.co.uk/s3fs-public/thumbnails/image/2014/03/25/12/eiffel.jpg">&nbsp; &nbsp; &nbsp;<div class="content-details fadeIn-bottom">&nbsp; &nbsp; &nbsp; &nbsp;<h3 class="content-title">Eiffel Tower</h3>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp;</a>&nbsp;</div>&nbsp; <div class="container">&nbsp; <div class="carousel">&nbsp; &nbsp; <div class="item a active">&nbsp; &nbsp; <div class="content-overlay"></div>&nbsp; &nbsp; <img src="https://www.insidehook.com/wp-content/uploads/2019/08/HERO-3.jpg?fit=1200%2C750" width="250px" height="200px">&nbsp; &nbsp; &nbsp;<div class="carousel_content-details carousel_fadeIn-bottom">&nbsp; &nbsp; &nbsp; &nbsp;<h3 class="carousel_content-title">Text 1</h3>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="item b">&nbsp; &nbsp; <div class="content-overlay"></div>&nbsp; &nbsp; <img src="https://static.independent.co.uk/s3fs-public/thumbnails/image/2014/03/25/12/eiffel.jpg" width="250px" height="200px">&nbsp; &nbsp; <div class="carousel_content-details carousel_fadeIn-bottom">&nbsp; &nbsp; &nbsp; &nbsp;<h3 class="carousel_content-title">Text 2</h3>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="item c">&nbsp; &nbsp; <div class="content-overlay"></div>&nbsp; &nbsp; <img src="https://nypost.com/wp-content/uploads/sites/2/2019/12/robberies-serious-crimes-spike-in-central-park-this-year.jpg?quality=80&strip=all" width="250px" height="200px">&nbsp; &nbsp; &nbsp;<div class="carousel_content-details carousel_fadeIn-bottom">&nbsp; &nbsp; &nbsp; &nbsp;<h3 class="carousel_content-title">Text 3</h3>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; </div>&nbsp; </div></div><div class="next">Next</div><div class="prev">Prev</div>&nbsp; <script src="script.js"></script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript