动画与图像在彼此的顶部

时间:2015-07-30 17:09:17

标签: 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>

1 个答案:

答案 0 :(得分:1)

我稍微调整了你的代码并使这个小提琴。 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 Code

$('.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;
})