Div显示自己的一半 - 使用swipe.js

时间:2013-08-05 21:54:00

标签: javascript html slider slide.js

试图找到一个我不需要jQuery来操作的滑块库。根据演示,我在同一个swipejs容器中有两个不同宽度的图像,

<div id='mSwipe' style='background-color:#ffb6c1;width:300px;height:250px;position:relative; left:800px;' class='swipe'>
  <div id="swipewrape" class='swipe-wrap'>
    <div id="slideone" style="text-align:right;"><img src="http://www.totallygaming.com/sites/default/files/rotor/Commercial%20Intelligence.JPG"/></div>
    <div id="slidetwo" style="text-align:right;"><img src="http://in-formatio.com/wp-content/uploads/2012/02/ihop-free-pancake-day-20091-600x250.jpg"/></div>
  </div>
</div>
<div style='text-align:center;padding-top:20px;'>
  <button onclick='state_change();mySwipe.prev()'>prev</button>
  <button onclick='state_change();mySwipe.next()'>next</button>
</div>

<script>
var state = 0;
function state_change() //Dumb state change function
{
   if(state)
   {
      state = 0;
      document.getElementById('mSwipe').style.width = "300px";
   }
   else
   {
     state = 1;
     document.getElementById('mSwipe').style.width = "600px";

   }
}

我放入一个简单的小状态状态机,看看如果我在滑动效果开始之前更改div宽度我能做什么。我把回调方法中的大小更改包括在swipejs中,首先为no无济于事。 我只是想在每次按下按钮时更改div大小,以便插入的图像适合它

按下按钮时,div会调整到合适的大小,但对于大图像(这是一堆漂亮的煎饼)来说,似乎是它会滑过并显示大图像的一半滑动div中两次。我可以回到更小的图像

如果我打开我的Chrome控制台,则会显示完整图片,而我没有其他问题

这是怎么回事?

enter image description here

1 个答案:

答案 0 :(得分:3)

锥,

我已经改变了你的代码,并为你创建了一个小提琴。 如果你稍后改变了对jQuery的想法,还会在Javascript上给出jQuery命令。

的JavaScript

// pure JS
var elem = document.getElementById('slider');
window.mySwipe1 = Swipe(elem, {
    // startSlide: 4,
    // auto: 3000,
    continuous: true,
    // disableScroll: true,
    // stopPropagation: true,
    // callback: function(index, element) {},
    // transitionEnd: function(index, element) {}
});
// with jQuery
// window.mySwipe = $('#mySwipe').Swipe().data('Swipe');

HTML

<div id='slider' class='swipe'>
    <div class='swipe-wrap'>
        <div>
            <img src="http://www.totallygaming.com/sites/default/files/rotor/Commercial%20Intelligence.JPG" />
        </div>
        <div>
            <img src="http://in-formatio.com/wp-content/uploads/2012/02/ihop-free-pancake-day-20091-600x250.jpg" />
        </div>
    </div>
</div>
<div style='text-align:center;padding-top:20px;'>
    <button class='prev' onclick='mySwipe1.prev()'>prev</button>
    <button class='next' onclick='mySwipe1.next()'>next</button>
</div>

CSS

.swipe {
    overflow: hidden;
    visibility: hidden;
    position: relative;
}
.swipe-wrap {
    overflow: hidden;
    position: relative;
}
.swipe-wrap > div {
    float:left;
    width:100%;
    position: relative;
}
.swipe-wrap > div > img {
    margin:10%;
    width:90%;
}
#slider {
    height: auto;
    width:50%;
    left:200px;
}

<强> EXAMPLE

编辑:更新了小提琴,添加了原始图像尺寸

<强> EXAMPLE 2