创建一个效果就像一个幻灯片用jquery显示另一个幻灯片

时间:2015-10-06 06:09:50

标签: jquery animation slide

我想创建一个滑块,其中显示以下效果https://codyhouse.co/demo/image-comparison-slider/index.html,当点击按钮幻灯片相互重新评估时。我为每个幻灯片创建了2个按钮并试图做这样的事情

jQuery('#movetohappyscene').click(function(){
            jQuery('.badscene').animate({ marginLeft: '-1024px','z-index':'1'}, 2000);  
            jQuery('.happyscene').animate({ 
                marginLeft: '0px', 
                'z-index' : '2' 
            }, 2000);   
        });
        jQuery('#movetobadscene').click(function(){
            jQuery('.happyscene').animate({ 'z-index':'1'}, 2000);
            jQuery('.badscene').animate({'z-index':'2', marginLeft: '0px'},    2000);
        });


 <div class="slidecontainer">
    <div class="badscene">
        <a id="movetohappyscene">Click to show happy scene</a>
        <img src="img/badsceneempty.png">
    </div>
    <div class="happyscene">
        <a id="movetobadscene">Click to show sad scene</a>
         <img src="img/goodscene.png">
    </div>
 </div>

以下是更新后的fiddle

但是效果和那个滑块不一样。我如何自定义我的js以便它们相似?

1 个答案:

答案 0 :(得分:1)

我的问题有一个类似的例子,但我是用纯css做的。

/**
 * Image slider with pure CSS
 */

.image-slider {
	position:relative;
	display: inline-block;
	line-height: 0;
}


.image-slider > div {
	position: absolute;
	top: 0; bottom: 0; left: 0;
	width: 25px;
	max-width: 100%;
	overflow: hidden;
	resize: horizontal;
}

/* Cross-browser resizer styling */
.image-slider > div:before {
	content: '';
	position: absolute;
	right: 0; bottom: 0;
	width: 13px; height: 13px;
	padding: 5px;
	background: linear-gradient(-45deg, black 50%, transparent 0);
	background-clip: content-box;
	cursor: ew-resize;
	-webkit-filter: drop-shadow(0 0 2px black);
	filter: drop-shadow(0 0 2px red);
}

.image-slider img {
	user-select: none;
	max-width: 400px;
}
<div class="image-slider">
  <div>
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-before.jpg" />
  </div>
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-after.jpg" />
</div>

相关问题