首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >css/js/jquery中的自动轮播

css/js/jquery中的自动轮播
EN

Stack Overflow用户
提问于 2021-08-23 08:28:40
回答 2查看 153关注 0票数 1

我需要你的帮助来创建一个旋转木马,将自动和连续转动。目标是它永远不会停止,以一种流畅的方式旋转,当你把鼠标放在其中一个元素上时,它就会停止旋转。我已经做了代码的一部分,它会自动转动,但模拟点击其中一个箭头,正如你所看到的,它会给出一个动画:当我在寻找连续的东西时,它会给出一个动画:减速-加速-减速。

我的代码:

代码语言:javascript
复制
var carousel = $(".carousel"),
    items = $(".item"),
    currdeg  = 0;

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

function rotate(e){
  if(e.data.d=="n"){
    currdeg = currdeg - 60;
  }
  if(e.data.d=="p"){
    currdeg = currdeg + 60;
  }
  carousel.css({
    "-webkit-transform": "rotateY("+currdeg+"deg)",
    "-moz-transform": "rotateY("+currdeg+"deg)",
    "-o-transform": "rotateY("+currdeg+"deg)",
    "transform": "rotateY("+currdeg+"deg)"
  });
    items.css({
    "-webkit-transform": "rotateY("+(-currdeg)+"deg)",
    "-moz-transform": "rotateY("+(-currdeg)+"deg)",
    "-o-transform": "rotateY("+(-currdeg)+"deg)",
    "transform": "rotateY("+(-currdeg)+"deg)"
  });
}

/*var btn = document.querySelector(".next");
console.log(btn);
setInterval(function(){
btn.click();
},700);*/
代码语言:javascript
复制
body {
  background: #333;
  padding: 70px 0;
  font: 15px/20px Arial, sans-serif;
}

.container {
  margin: 0 auto;
  width: 120px;
  height: 200px;
  position: relative;
  perspective: 1000px;
}

.carousel {
  height: 100%;
  width: 100%;
  position: absolute;
  transform-style: preserve-3d;
  transition: transform 1s;
}
.carousel div {
  transform-style: preserve-3d;
  
}

.item {
  display: block;
  position: absolute;
  width: 120px;
  height: 200px;
  line-height: 200px;
  font-size: 5em;
  text-align: center;
  color: #FFF;
  opacity: 0.95;
  border-radius: 10px;
  transition: transform 1s;
  -webkit-animation: rotating 1s linear infinite;
}

.a {
  transform: rotateY(0deg) translateZ(250px);
}
.b {
  transform: rotateY(60deg) translateZ(250px) rotateY(-60deg);
}
.c {
  transform: rotateY(120deg) translateZ(250px)  rotateY(-120deg);
}
.d {
  transform: rotateY(180deg) translateZ(250px) rotateY(-180deg);
}
.e {
  transform: rotateY(240deg) translateZ(250px) rotateY(-240deg);
} 
.f {
  transform: rotateY(300deg) translateZ(250px) rotateY(-300deg);
}

.next, .prev {
  color: #444;
  position: absolute;
  top: 240px;
  padding: 1em 2em;
  cursor: pointer;
  border-radius: 5px;
}
.next:hover, .prev:hover { color: #000; }
.next:active, .prev:active {
  top: 240px;
}
.next { right: 5em; }
.prev { left: 5em; }
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="style.css" rel="stylesheet">
    <title>Document</title>
</head>
<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <div class="container">
    <div class="carousel" id="carousel2">
      <div class="a">
        <div class="item"><a href="https://chateaupouzols.com/chardonnay/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v1-2.png"/></a></div>
      </div>
      <div class="b">
        <div class="item"><a href="https://chateaupouzols.com/1437-2/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/08/v2-2-2.png"/></a></div>
      </div>
      <div class="c">
        <div class="item"><a href="https://chateaupouzols.com/cabernet-sauvignon/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v3-2.png"/></a></div>
      </div>
      <div class="d">
        <div class="item"><a href="https://chateaupouzols.com/merlot/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v4-2.png"/></a></div>
      </div>
      <div class="e">
        <div class="item"><a href="https://chateaupouzols.com/la-gardye/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v5-2.png"/></a></div>
      </div>
      <div class="f">
        <div class="item"><a href="https://chateaupouzols.com/syrah/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v6-2.png"/></a></div>
      </div>
    </div>
  </div>
  <div class="next"><img style="width:30px;" src="https://chateaupouzols.com/wp-content/uploads/2021/06/fleshD-3.png"/></div>
  <div class="prev"><img style="width:30px;" src="https://chateaupouzols.com/wp-content/uploads/2021/06/fleshG-3.png"/></div>

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

提前感谢您的回复,祝您愉快!

EN

回答 2

Stack Overflow用户

发布于 2021-08-23 08:36:23

使用jQuery添加点击触发器。

setInterval 5000将每5秒触发一次点击。

代码语言:javascript
复制
setInterval(function(){
    $(".next").trigger('click')
}, 5000)

票数 0
EN

Stack Overflow用户

发布于 2021-08-23 09:09:24

我会使用jQuery的悬停方法来实现这一点。此方法可以接受一到两个参数,第一个参数称为handlerIn,它可以转换为悬停状态、鼠标输入等,以及对应于“鼠标离开”事件的handlerOut

在window onLoad()上,您可以使用返回间隔的setInterval(),您可以传递该间隔以清除鼠标悬停时的间隔。

代码语言:javascript
复制
window.onload = function()
{
  refresh = setInterval(function (){
      $(".next").trigger('click')
  }, 1000);
};

$('#carousel2').hover(mouseEnter, mouseLeave);
function mouseEnter() {
     clearInterval(refresh);
};
function mouseLeave() {
       refresh = setInterval(function (){
      $(".next").trigger('click')
  }, 1000);
};

代码语言:javascript
复制
var carousel = $(".carousel"),
    items = $(".item"),
    currdeg  = 0;

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

function rotate(e){
  if(e.data.d=="n"){
    currdeg = currdeg - 60;
  }
  if(e.data.d=="p"){
    currdeg = currdeg + 60;
  }
  carousel.css({
    "-webkit-transform": "rotateY("+currdeg+"deg)",
    "-moz-transform": "rotateY("+currdeg+"deg)",
    "-o-transform": "rotateY("+currdeg+"deg)",
    "transform": "rotateY("+currdeg+"deg)"
  });
    items.css({
    "-webkit-transform": "rotateY("+(-currdeg)+"deg)",
    "-moz-transform": "rotateY("+(-currdeg)+"deg)",
    "-o-transform": "rotateY("+(-currdeg)+"deg)",
    "transform": "rotateY("+(-currdeg)+"deg)"
  });
}

/*var btn = document.querySelector(".next");
console.log(btn);
setInterval(function(){
btn.click();
},700);*/

window.onload = function()
{
  refresh = setInterval(function (){
      $(".next").trigger('click')
  }, 1000);
};

$('#carousel2').hover(mouseEnter, mouseLeave);
function mouseEnter() {
     clearInterval(refresh);
};
function mouseLeave() {
       refresh = setInterval(function (){
      $(".next").trigger('click')
  }, 1000);
};
代码语言:javascript
复制
body {
  background: #333;
  padding: 70px 0;
  font: 15px/20px Arial, sans-serif;
}

.container {
  margin: 0 auto;
  width: 120px;
  height: 200px;
  position: relative;
  perspective: 1000px;
}

.carousel {
  height: 100%;
  width: 100%;
  position: absolute;
  transform-style: preserve-3d;
  transition: transform 1s;
}
.carousel div {
  transform-style: preserve-3d;
  
}

.item {
  display: block;
  position: absolute;
  width: 120px;
  height: 200px;
  line-height: 200px;
  font-size: 5em;
  text-align: center;
  color: #FFF;
  opacity: 0.95;
  border-radius: 10px;
  transition: transform 1s;
  -webkit-animation: rotating 1s linear infinite;
}

.a {
  transform: rotateY(0deg) translateZ(250px);
}
.b {
  transform: rotateY(60deg) translateZ(250px) rotateY(-60deg);
}
.c {
  transform: rotateY(120deg) translateZ(250px)  rotateY(-120deg);
}
.d {
  transform: rotateY(180deg) translateZ(250px) rotateY(-180deg);
}
.e {
  transform: rotateY(240deg) translateZ(250px) rotateY(-240deg);
} 
.f {
  transform: rotateY(300deg) translateZ(250px) rotateY(-300deg);
}

.next, .prev {
  color: #444;
  position: absolute;
  top: 240px;
  padding: 1em 2em;
  cursor: pointer;
  border-radius: 5px;
}
.next:hover, .prev:hover { color: #000; }
.next:active, .prev:active {
  top: 240px;
}
.next { right: 5em; }
.prev { left: 5em; }
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="style.css" rel="stylesheet">
    <title>Document</title>
</head>
<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <div class="container">
    <div class="carousel" id="carousel2">
      <div class="a">
        <div class="item"><a href="https://chateaupouzols.com/chardonnay/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v1-2.png"/></a></div>
      </div>
      <div class="b">
        <div class="item"><a href="https://chateaupouzols.com/1437-2/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/08/v2-2-2.png"/></a></div>
      </div>
      <div class="c">
        <div class="item"><a href="https://chateaupouzols.com/cabernet-sauvignon/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v3-2.png"/></a></div>
      </div>
      <div class="d">
        <div class="item"><a href="https://chateaupouzols.com/merlot/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v4-2.png"/></a></div>
      </div>
      <div class="e">
        <div class="item"><a href="https://chateaupouzols.com/la-gardye/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v5-2.png"/></a></div>
      </div>
      <div class="f">
        <div class="item"><a href="https://chateaupouzols.com/syrah/"><img src="https://chateaupouzols.com/wp-content/uploads/2021/06/v6-2.png"/></a></div>
      </div>
    </div>
  </div>
  <div class="next"><img style="width:30px;" src="https://chateaupouzols.com/wp-content/uploads/2021/06/fleshD-3.png"/></div>
  <div class="prev"><img style="width:30px;" src="https://chateaupouzols.com/wp-content/uploads/2021/06/fleshG-3.png"/></div>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68889460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档