javascript - 图像相互叠加的动画

标签 javascript jquery html css

我正在尝试实现一个小的图像动画,以半透明的方式将图像叠加在一起,以便可以同时看到所有图像。

此外,按钮应该可以打开和关闭,这样可以实现不同的图像或颜色组合。

目前我只能使用每个按钮一次,而且只能看到 2 张图片。

( Code Pen )

function crossfadeImages() {
  var $active = $('#show-img .active');
  var $next = $('#show-img .next');

  $active.animate({
    opacity: "0.5"
  }, 500, function() {
    $(this).stop('style');
  });
}

$('.buttons a.page').first().addClass('activeButton');

$('.buttons a.page').click(function() {
  $('.buttons a.page').removeClass('activeButton');
  $(this).addClass('activeButton');
  if (!$('#show-img').find('img').eq($(this).attr('rel')).hasClass('active')) { //if the clicked image is not already the active image

    $('#show-img').find('img').eq($(this).attr('rel')).addClass('next'); //flag the image as the next one
    crossfadeImages();
  }
  return false;
})
img {
  width: 65%;
  opacity: 1;
}
/*image show*/

#show-img {
  position: relative;
}
#show-img img {
  position: absolute;
  z-index: 1;
  background-color: #fff;
}
#show-img img.active {
  z-index: 3
}
#show-img img.next {
  z-index: 2
}
img {
  display: block;
}
.button {
  width: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">

  <div class="large-5 columns buttons">
    <br>
    <br>
    <br>
    <br>
    <h4>the possebilities</h4> 
    <br>
    <br>
    <br>
    <br>
    <a class="page activeButton radius blue button" href="#" rel="0">button1</a>
    <br>
    <a class="page radius green button" href="#" rel="1">button2</a>
    <br>
    <a class="page radius red button" href="#" rel="2">button3</a>
    <br>
    <a class="page radius black button" href="#" rel="3">button4</a>

  </div>

  <div id="show-img" class="large-7 columns show-img">
    <h1>main header</h1>
    <img class="active" src="http://vollwebdesign.com/foundation- testing/blue.png">
    <img src="http://vollwebdesign.com/foundation-testing/rosa.png">
    <img src="http://vollwebdesign.com/foundation-testing/yellow.png">
    <img src="http://vollwebdesign.com/foundation-testing/orange.png">

  </div>
</div>

最佳答案

我稍微调整了你的代码并制作了这个 fiddle 。 https://jsfiddle.net/osha90/g3Lwst0n/ .希望这就是您要找的东西

    <div class="row">

   <div class="large-5 columns buttons">
      <br> <br> <br> <br>
      <h4>the possebilities</h4> <br> <br> <br> <br>
      <a class="page activeButton radius blue button" href="#" rel="0">button1</a><br>
      <a class="page radius green button" href="#" rel="1">button2</a><br>
      <a class="page radius red button" href="#" rel="2">button3</a><br>
      <a class="page radius black button" href="#" rel="3">button4</a>
  </div>

   <div id="show-img" class="large-7 columns show-img">
      <h1>main header</h1>
      <img src="http://vollwebdesign.com/foundation-testing/blue.png">
      <img src="http://vollwebdesign.com/foundation-testing/rosa.png">
      <img src="http://vollwebdesign.com/foundation-testing/yellow.png">
      <img src="http://vollwebdesign.com/foundation-testing/orange.png">       

  </div>
</div>

CSS代码

/*image show*/
#show-img {
  position: relative;
}
#show-img img {
  position: absolute;
  z-index: 1;
  background-color: transparent;
  width: 65%;
  opacity: 0;
  transition: all 0.5s ease-out;
}
#show-img img:first-of-type {
  opacity: 1;
}
#show-img img.active{
  opacity: 0.7;
}
.button {
  width: 200px;
}

JS代码

$('.buttons a.page').click(function() {
  if($("#show-img img:eq(0)").css("opacity") == 1){
    $("#show-img img:eq(0)").css("opacity","0.7");
  }
  var index = $(this).attr("rel");
  var correspondingImage = $("#show-img img:eq(" + index + ")");
  if ($(this).hasClass("active")) {
    $(this).removeClass("active");
    correspondingImage.removeClass("active");
  } else {
    $(this).addClass("active")
    correspondingImage.addClass("active");
  }
  return false;
})

关于javascript - 图像相互叠加的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31730030/

相关文章:

javascript - 用于单个页面上单个音频元素的 jQuery 预加载器

javascript - Fullcalendar events.php 不返回 JSON 对象

javascript - 浏览推文(框)+三个js

html - 如何使用 CSS 在 HTML 文档中插入换行符

javascript - 嵌套函数的用途或优点是什么?

javascript - 单击时会显示 jQuery 选项卡和选项卡内容?

javascript - 防止在特定屏幕尺寸的智能手机上滚动

javascript - 使用 jQuery 选择不相关的元素

javascript - 如果返回值为空,则隐藏一行

javascript - 向文档动态添加 CSS 规则时等待应用样式