2014-09-29 54 views
1

我是jQuery的新手,我摆弄着从数组中加载图像并将结果发布到div的排序滑块。我有它的方式设置它加载图像标记和图像源,并将其放置到一个空的div。现在,我已经看到jQuery中与数组结合使用的任何动画格式都大不相同。我应该提到,我并不是在寻找优雅的,而是仅仅是有效的代码。动画从图像数组转换?

基本上,是否有可能将转换与我现在的做法相结合?如果是这样,怎么样?没有什么特别的,只是淡入淡出效果。

jsFiddle so far

var imgArray = 
    ['<img src="http://placehold.it/400x300/cf5">', 
    '<img src="http://placehold.it/400x300/555">', 
    '<img src="http://placehold.it/400x300/f0f">', 
    '<img src="http://placehold.it/400x300/05b">'] 

    counter = -1; 

    $('#nextimage').click(function() { 
    counter = (counter + 1) % imgArray.length; 
    console.log(imgArray[counter]); 
      document.getElementById("result1").innerHTML = (imgArray[counter]); 
    }); 

    $('#previmage').click(function() { 
    counter = (counter - 1); 
    console.log(imgArray[counter]); 
      document.getElementById("result1").innerHTML = (imgArray[counter]); 
    }); 

HTML

<div class="container"> 
    <div class="slider_wrapper"> 
     <div id="slider"> 
     <div class="img-wrap"> 
      <div id="result1"></div> 
     </div> 
     </div> 
    </div> 
    </div> 
    <div id="footer"> 
     <a href="#" id="previmage"><img title="Previous Image" alt="prev image" src="https://dl.dropboxusercontent.com/u/65639888/image/prev.png"></a> 
     <a href="#" id="nextimage"><img title="Next Image" alt="next image" src="https://dl.dropboxusercontent.com/u/65639888/image/next.png"></a> 
    </div> 

CSS

body { 
    background-color:black; 
} 
.container{ 
    padding:5px; 
    border:1px dashed black; 
    -webkit-box-sizing:border-box; 
    -moz-box-sizing:border-box; 
    box-sizing:border-box; 
    background: black; 
} 
.slider_wrapper{ 
    overflow:hidden; 
    position:relative; 
    max-width:100%; 
    height:auto; 
    top:auto; 
    border-style: dashed; 
    border-color: white; 
} 

#slider{ 
    position: relative; 
    list-style: none; 
    overflow: hidden; 
    margin:0px; 
    border-style: solid; 
    border-color: green; 
} 
#slider img{ 
    width: 100%; 
    height:auto; 
    position: relative; 
    float: left; 
} 
.nvgt{ 
    position:absolute; 
    top: 0px; 
    height: 100%; 
    width: 50%; 
    opacity: 0; 
} 

#footer { 
    position: fixed; 
    bottom: 0; 
    width: 100%; 
    background: BLACK; 
    line-height: 2; 
    text-align: center; 
    color: #FFFFFF; 
    font-size: 14px; 
    font-weight: bold; 
    text-shadow: 0 1px 0 #84BAFF; 
    box-shadow: 0 0 15px #FFFFFF; 
    opacity: 0.1; 
    padding:0; 
    margin:0; 
} 

#footer img { 
    padding-top:10px; 
    padding-bottom: 5px; 
    padding-right:20px; 
    padding-left:20px; 
    height: 30px; 
} 

#footer:hover { 
    opacity: 1; 
} 

ul, menu, dir, html, body { 
    -webkit-margin-end: 0px; 
    -webkit-padding-start: 0px; 
    margin:0px; 
} 
+0

你应该总是寻找优雅。 – artm 2014-09-29 13:59:47

+0

你在寻找自动转换吗? – 2014-09-29 14:09:27

回答

0

一个和下一个按钮,淡出效果

var imgArray = ['<img src="http://placehold.it/400x300/cf5">', 
      '<img src="http://placehold.it/400x300/555">', 
      '<img src="http://placehold.it/400x300/f0f">', 
      '<img src="http://placehold.it/400x300/05b">'] 

counter = -1; 

function imgTransition() { 
    $('#result1').fadeOut(300, function() { 
     $('#result1').html(imgArray[counter]); 
     $('#result1 > img').on('load', function() { 
      $('#result1').fadeIn(500); 
     }); 
    }); 
}; 

function next() { 
    counter = (counter + 1) % imgArray.length; 
    console.log(counter); 
    imgTransition(); 
}; 

next(); 

$('#nextimage').click(next); 

$('#previmage').click(function() { 
    if (counter == '0') { 
     counter = imgArray.length; 
    } 
    counter = (counter - 1); 
    console.log(imgArray[counter]); 
    imgTransition(); 
}); 

JSFIIDLE

1


see jsfiddle

var imgArray = 
    ['<img src="http://placehold.it/400x300/cf5">', 
    '<img src="http://placehold.it/400x300/555">', 
    '<img src="http://placehold.it/400x300/f0f">', 
    '<img src="http://placehold.it/400x300/05b">'], 

    counter = -1; 
var animate = false; //stopping invalid click during animation 

function animatable() { 
    $('#result1').slideUp(300, function() { 
     $('#result1').html(imgArray[counter]); 
     $('#result1 > img').on('load', function() { 
      //$('#result1').fadeIn(500); 
      $('#result1').animate({ 
       height: "toggle" 
      }, 1000 , function(){ 
       animate = false; 
      }); 
     }); 
    }); 
}; 
function next(){ 
    if(!animate){ 
     animate = true; 
    counter = (counter + 1) % imgArray.length; 
    console.log(counter); 
     $("#result1").fadeIn().html(imgArray[counter]); 
    animatable(); 
    } 
}; 

next(); 

$('#nextimage').click(next); 

$('#previmage').click(function() { 
    if(!animate){ 
     animate = true; 
     if(counter=='0'){ 
     counter= imgArray.length; 
     } 
    counter = (counter - 1); 
    console.log(imgArray[counter]); 
     $("#result1").html(imgArray[counter]); 
    animatable(); 
    } 
}); 

update with animatable

+0

以前的点击工作的某些限制 – 2014-09-29 14:00:31

+0

对不起,我应该在帖子中澄清,我正在寻找过渡效果。编辑来反映这一点。 – GoldCoin 2014-09-29 14:03:36

+0

好的,一切皆有可能。 *我会举一个例子* – AvrilAlejandro 2014-09-29 14:07:24

1

关键是要等到已经加载了正常工作。

var imgArray = 
    ['<img src="http://placehold.it/400x300/cf5">', 
    '<img src="http://placehold.it/400x300/555">', 
    '<img src="http://placehold.it/400x300/f0f">', 
    '<img src="http://placehold.it/400x300/05b">'] 

counter = -1; 

function imgTransition() { 
    $('#result1').fadeOut(300, function() { 
     $('#result1').html(imgArray[counter]); 
     $('#result1 > img').on('load', function() { 
      $('#result1').fadeIn(500); 
     }); 
    }); 
}; 

$('#nextimage').click(function() { 
    counter = (counter + 1) % imgArray.length; 
    console.log(imgArray[counter]); 
    imgTransition(); 
}); 

$('#previmage').click(function() { 
    counter = (counter - 1); 
    console.log(imgArray[counter]); 
    imgTransition(); 
}); 

JSFiddle

+0

为什么我有两个downvotes?我错过了什么? – 2014-09-29 14:32:58

+0

一旦我有业力upvote我会这样做。不知道为什么它被拒绝投票。很好地回答了我的问题。 – GoldCoin 2014-09-29 14:54:40

+0

@GoldCoin没有问题的队友,我不关心downvotes。我只是想知道我是否做错了什么。很高兴我能帮上忙。 – 2014-09-29 14:58:20