【问题标题】:Trying to create a carousel effect with jQuery尝试使用 jQuery 创建轮播效果
【发布时间】:2014-07-14 12:31:10
【问题描述】:

页面加载时,共有四个字符,其中仅显示一个。 有两个箭头。一个在左边,一个在右边。 单击左箭头时,当前字符淡出,前一个字符淡入。单击右箭头时,当前字符淡出,下一个字符淡入。 我已经弄清楚如何淡出屏幕中的当前字符,但不知道如何在单击箭头时淡入下一个或上一个字符。

这是我为说明问题而创建的小提琴:http://jsfiddle.net/9K7bf/32/

这里是代码:

HTML:

<section id="characters">
    <div id="one" class="character"></div>
    <div id="two" class="character"></div>
    <div id="three" class="character"></div>
    <div id="four" class="character"></div>
</section>

<div id="arrow-left"></div>
<div id="arrow-right"></div>

CSS:

#characters{
    width: 100%;
    height: 100px;
}
#one{
    height: 50px;
    width: 50px;
    display: block;
    background-color: green;
    margin: 100px auto;
}
#two{
    height: 50px;
    width: 50px;
    display: none;
    background-color: blue;
    margin: 100px auto;
}
#three{
    height: 50px;
    width: 50px;
    display: none;
    background-color: purple;
    margin: 100px auto;
}
#four{
    height: 50px;
    width: 50px;
    display: none;
    background-color: black;
    margin: 100px auto;
}
#arrow-left{
    height: 25px;
    width: 25px;
    display: block;
    background-color: black;
    float: left;
}
#arrow-right{
    height: 25px;
    width: 25px;
    display: block;
    background-color: black;
    float: right;
}

jQuery:

$(document).ready(function(){
  $("#arrow-right").on('click', function(){
    $(".character").fadeOut().this();
  });  
    $("#arrow-left").on('click', function(){
    $(".character").fadeOut().this();
  });
});

【问题讨论】:

  • +1 表示小提琴和一般的学习意愿
  • 您在寻找类似this 的东西吗?如果是,我将在答案中解释做了什么。并且出于同样的原因 +1,原因与 amenthes 所说的相同。
  • 非常感谢。我不想把我的问题扔在那里以获得快速答案。该平台旨在记录问题和答案,以便其他人也可以从中学习。我这是我的归因方式:)

标签: javascript jquery html css


【解决方案1】:

您可以通过执行以下操作来达到效果。实际上,我们必须使用计数器找出当前项目是什么,然后继续遍历它。

另外一点,您还可以通过为其分配class='arrow' 并在其下提供所有常用属性来避免箭头框中的重复。

$(document).ready(function() {
  var i = 0; // Counter variable to keep track of the current item
  $("#arrow-right").on('click', function() {
    $(".character").eq(i).fadeOut('fast'); // Quickly fade out the current element
    i = i < 3 ? i + 1 : 0; // Increment counter till it reaches 3 (because element index is from 0 to 3). If it reaches 3 then we reset to 0 to loop back again.
    $(".character").eq(i).fadeIn('slow'); // Slowly fade in the next element. Note i here is the next element because we have already incremented the counter.
  });
  $("#arrow-left").on('click', function() {
    $(".character").eq(i).fadeOut('fast');
    i = i > 0 ? i - 1 : 3; // Same as for the right click except here the logic is reverse as we have to go back.
    $(".character").eq(i).fadeIn('slow');
  });
});
#characters {
  width: 100%;
  height: 100px;
  position: relative; /* Need because the characters would be absolutely positioned relative to their parent box */
}
.character { /* Created this class to put in all common properties to avoid repetition */
  height: 50px;
  width: 50px;
  position: absolute; /* This is required because all elements have to be positioned one on top of another */
  left: 50%; /* Required for positioning the boxes */
  top: 50%;  /* Required for positioning the boxes */
}
#one {
  display: block;
  background-color: green;
}
#two {
  display: none;
  background-color: blue;
}
#three {
  display: none;
  background-color: purple;
}
#four {
  display: none;
  background-color: black;
}
.arrow { /* Common class for common properties */
  height: 25px;
  width: 25px;
  display: block;
  background-color: black;
}
#arrow-left {
  float: left;
}
#arrow-right {
  float: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<section id="characters">
  <div id="one" class="character"></div>
  <div id="two" class="character"></div>
  <div id="three" class="character"></div>
  <div id="four" class="character"></div>
</section>
<div id="arrow-left" class='arrow'></div> <!-- Note the addition of class -->
<div id="arrow-right" class='arrow'></div>

Fiddle Demo

【讨论】:

  • 完全按照我的意愿工作。非常感谢您对代码背后的解释。这正是我想知道的。
【解决方案2】:

我制作了您的 jsfiddle 的更新版本。我补充说,一件事是一个计数器,指示当前处于活动状态的内容(请参阅以下代码中的 activeItem)。

这是为了向您展示一个新的方向,而不是为您编写所有代码。接下来您可能想要解决的问题是:

  • 有很多重复,这可能会更优雅。
  • 它不检测环绕(小于零,大于三(记住:javascript 从零开始计数))
  • 您无法在具有所有 ID 的页面中重复使用此多次。
  • 淡入项目会跳来跳去,因为它们是display:block(CSS 术语)。这意味着,只要它们是可见的,它们就会在页面上保留它们的位置并取代其他东西。这可以通过 CSS 中的position: absoluteposition: fixed 来解决,具体取决于您周围的代码。

这里是:

$(document).ready(function(){
    // store what's current
    var activeItem = 0;

    $("#arrow-right").on('click', function(){
        $(".character").eq(activeItem).fadeOut();
        activeItem++;
        $(".character").eq(activeItem).fadeIn();
    });  

    $("#arrow-left").on('click', function(){
        $(".character").eq(activeItem).fadeOut();
        activeItem--;
        $(".character").eq(activeItem).fadeIn();
    });
});

newer version the jsfiddle

【讨论】:

  • - 完全同意代码可以更优雅。 -我非常喜欢@Harry 所做的例子。这是一个循环,你不是。 - 感谢您对 CSS 的解释。很高兴知道这个小细节。
【解决方案3】:

使用 .next() 和 .prev()。 注意:这仅在您不想单击最后一个返回开始时才有效。

$(document).ready(function(){

$("#arrow-right").on('click', function(){
    var next = $(".character:visible").next().attr("id");
    $(".character").fadeOut();
    $("#"+next).fadeIn();
});  
$("#arrow-left").on('click', function(){
    var prev = $(".character:visible").prev().attr("id");
    $(".character").fadeOut();
    $("#"+prev).fadeIn();
});
});

编辑:在最后一个按钮上单击下一步会破坏它,所以我添加了 if 语句。如果是最后一个按钮,您可以使用 else 更改按钮的 css。

$(document).ready(function(){

    $("#arrow-right").on('click', function(){
        var next = $(".character:visible").next().attr("id");
        if(next){
            $(".character").fadeOut();
            $("#"+next).fadeIn();
        }
    });  
    $("#arrow-left").on('click', function(){
        var prev = $(".character:visible").prev().attr("id");
        if(prev){
            $(".character").fadeOut();
           $("#"+prev).fadeIn();
        }
    });
});

【讨论】:

  • +1。正如 Brain 在 amenthes 的回答中提到的那样,他正在寻找一个循环。但是,如果不需要循环,这是一个很好的替代选择。
  • 错过了循环部分。我倾向于不阅读整本书。对不起。
  • 其实可以实现循环。如果你把它改成: if(!next){next=1;} 这会让你回到第一个。
  • 非常正确,我也为此创建了一个demo。如果您愿意,您可以将其添加到您的答案中:)
【解决方案4】:

您的 javascript 完全按照您的要求执行。您单击箭头,角色就会淡出。您在脚本中没有分配任何变量来定义其他字符。您只需在 css 中引用它们即可。

有了这么多免费的 jquery 滑块,为什么不直接使用像 Nivo 这样的插件呢?或谷歌“如何从头开始构建 jquery 滑块”。那里有很多教程。

【讨论】:

  • 好点!但是我正在尝试自己编写代码而不是使用插件,这样我就可以提高我的编码技能。我知道淡出代码工作正常。正如我在问题中描述的那样,我希望我能得到一些关于 fadeIn 部分的帮助:)
  • 这就是为什么我建议一个教程,javascript比淡入复杂得多。你必须将字符定义为变量。
  • 将字符定义为变量是让我有不同想法的部分。我实际上认为答案与 .hasClass() 有关,但@Harry 解释得非常详细。我在不断地练习JS,但这只是超出了我现阶段的水平。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 2012-12-11
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多