Jquery Slider左右滑动问题

时间:2015-10-03 20:35:42

标签: javascript jquery slider

嗨,我在创建jquery滑块时遇到问题。我正在尝试使用animate将图像向右和向左滑动。我无法弄清问题是什么。为了让图像左右滑动,我做错了什么?谢谢!

我添加了一个代码段,但似乎lorempixel.com图片没有显示在这里。

'use strict';

$(document).ready(function() {


  var total = 11;

  for (var i = 1; i < total; i++) {
    $('#slideshow .pics').append('<img src="http://lorempixel.com/500/300/food/' + i + '" />');
    $('#slideshow .pics').append('<b>Slide ' + i + '</b>.'); //add text to slide
    $('.circle').append('<i class="fa fa-circle"></i>');
  }

  function slideAnimationLeft(targetLeft) {
    var Pictures = $('.pics');

    return function() {
      Pictures.children('div:first').animate({
        'left': targetLeft + 'px'
      }, 1000, function() {
        Pictures.children('div:first').appendTo(Pictures).css('marginTop', '0px').fadeIn(2000);
      });
    };
  };

  /*   function slideAnimationRight(targetRight) {
         return function() {
             $('.pics img:nth-child').animate({
                     'right': targeRight + 'px'},
                 1000);
         };
     };*/

  $('.left').click(slideAnimationLeft(-300));
  $('.right').click(slideAnimationRight(300));


});
/* http://meyerweb.com/eric/tools/css/reset/
   v2.0 | 20110126
   License: none (public domain)
*/

/* line 6, ../sass/_reset.scss */

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */

/* line 27, ../sass/_reset.scss */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}
/* line 31, ../sass/_reset.scss */

body {
  line-height: 1;
}
/* line 34, ../sass/_reset.scss */

ol,
ul {
  list-style: none;
}
/* line 37, ../sass/_reset.scss */

blockquote,
q {
  quotes: none;
}
/* line 40, ../sass/_reset.scss */

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}
/* line 45, ../sass/_reset.scss */

table {
  border-collapse: collapse;
  border-spacing: 0;
}
/* line 50, ../sass/_reset.scss */

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
/* line 6, ../sass/styles.scss */

#slideshow {
  width: 500px;
  height: 300px;
  margin: 0 auto;
  overflow: hidden;
  margin-top: 10%;
}
/* line 13, ../sass/styles.scss */

#slideshow > img {
  position: absolute;
  display: block;
}
/* line 18, ../sass/styles.scss */

#slideshow .pics {
  z-index: 1;
}
/* line 22, ../sass/styles.scss */

#slideshow .control {
  z-index: 2;
}
/* line 26, ../sass/styles.scss */

#slideshow > div {
  width: 500px;
  height: 300px;
  overflow: hidden;
  position: absolute;
}
/* line 34, ../sass/styles.scss */

.circle {
  text-align: center;
}
/* line 40, ../sass/styles.scss */

i.fa-circle {
  opacity: 0.7;
  margin-top: 20%;
  padding: 2px;
}
/* line 48, ../sass/styles.scss */

i.fa-angle-left {
  margin-top: 20%;
  opacity: 0.7;
  z-index: 100;
}
/* line 56, ../sass/styles.scss */

i.fa-angle-right {
  margin-top: 20%;
  margin-left: 87.75%;
  opacity: 0.7;
  z-index: 100;
}
<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title>Assignment 2</title>
  <link rel="stylesheet" href="stylesheets/styles.css">
  <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
</head>

<body>
  <div id="slideshow">
    <div class="control">
      <a href="#" class="left"><i class="fa fa-angle-left fa-5x left"></i></a>
      <a href="#" class="right"><i class="fa fa-angle-right fa-5x right"></i></a>

      <div class="circle">
      </div>

    </div>
    <div class="pics">

    </div>
  </div>

  <script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
  <script type="text/javascript" src="bower_components/jquery-ui/jquery-ui.js"></script>
  <script type="text/javascript" src="main.js"></script>


</body>

</html>

1 个答案:

答案 0 :(得分:1)

以下是一个有效的实例:http://jsfiddle.net/hxwfodd3/

这些是我为解决您的代码遇到的问题所做的主要事情:

$('.left').click(slideAnimationLeft(-300));

这会导致在页面加载后立即运行slideAnimationLeft(-300),这在这种情况下不是您想要的。

如果用户点击,您需要添加function()才能生效。

$('.left').click(function() { slideAnimationLeft(-300) } );

其次,因为你有两个div.left,所以该函数运行两次。 这将解决它

$('.left >').click(function() { slideAnimationLeft(-300) } );

我看到你试图找到&#34;这里第一张图片的div:Pictures.children('div:first').animate({ 虽然div不是首先创造的。

将您的代码更改为:

$('#slideshow .pics').append('<div>');
$('#slideshow .pics div:last').append('<img src="http://lorempixel.com/500/300/food/' + i + '" />');
$('#slideshow .pics div:last').append('<b>Slide ' + i + '</b>.'); //add text to slide
$('#slideshow .pics').append('</div>');

由于缺少css代码,很酷的滑动效果无效:

#slideshow .pics div {
   position:relative;
}

附加后重置left。改变这个:

Pictures.children('div:first').appendTo(Pictures).css('marginTop', '0px').fadeIn(2000);

进入这个:

Pictures.children('div:first').appendTo(Pictures).css('marginTop', '0px').css('left', '0px').fadeIn(2000);

实例:http://jsfiddle.net/hxwfodd3/