在x轴方向上更改jquery图像滑块的功能

时间:2017-01-10 14:31:40

标签: jquery html css3

嗨我已经制作了一个没有任何插件的纯jquery滑块,该滑块的功能是0到1的不透明度,但我想改变滑块在x轴方向滑动像bootstrap carousel,任何人都可以建议我如何滑动在x轴上,提前致谢

Bootstrap carousel link

HTML代码

<div id="container">
                    <header>
                        <h1>jquery Content Slider</h1>
                    </header>
                    <img src="images/prev.png" alt="previous" id="prev">
                    <div id="slider">
                        <div class="slide">
                            <div class="slide-copy">
                                <h1>Slider One</h1>
                                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt iste, et, modi error debitis vitae. Facere, quaerat voluptate inventore minus suscipit necessitatibus. Doloribus adipisci sit, sapiente incidunt, aut sint labore!</p>
                            </div>
                            <img src="http://placehold.it/940x350/FF0267/007FFF">
                        </div>

                        <div class="slide">
                            <div class="slide-copy">
                                <h1>Slider Two</h1>
                                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt iste, et, modi error debitis vitae. Facere, quaerat voluptate inventore minus suscipit necessitatibus. Doloribus adipisci sit, sapiente incidunt, aut sint labore!</p>
                            </div>
                            <img src="http://placehold.it/940x350/FF0267/007FFF">
                        </div>

                        <div class="slide">
                            <div class="slide-copy">
                                <h1>Slider Three</h1>
                                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt iste, et, modi error debitis vitae. Facere, quaerat voluptate inventore minus suscipit necessitatibus. Doloribus adipisci sit, sapiente incidunt, aut sint labore!</p>
                            </div>
                            <img src="http://placehold.it/940x350/FF0267/007FFF">
                        </div>

                        <div class="slide">
                            <div class="slide-copy">
                                <h1>Slider Four</h1>
                                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt iste, et, modi error debitis vitae. Facere, quaerat voluptate inventore minus suscipit necessitatibus. Doloribus adipisci sit, sapiente incidunt, aut sint labore!</p>
                            </div>
                            <img src="http://placehold.it/940x350/FFFFFF/808080">
                        </div>

                        <div class="slide">
                            <div class="slide-copy">
                                <h1>Slider Five</h1>
                                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt iste, et, modi error debitis vitae. Facere, quaerat voluptate inventore minus suscipit necessitatibus. Doloribus adipisci sit, sapiente incidunt, aut sint labore!</p>
                            </div>
                            <img src="http://placehold.it/940x350/FF0267/007FFF">
                        </div>
                    </div>
                    <img src="images/next.png" alt="next" id="next">
                </div>

css代码

                    body {
                    margin: 0;
                    padding: 0;
                    font-family: roboto;
                    line-height: 1.2em;
                    font-family: 'roboto';
                    background: #333;
                    color: #fff;
                }

                a {
                    color: #fff;
                    text-decoration: none;
                }

                h1 {
                    text-align: center;
                    margin-bottom: 20px;
                }

                #container {
                    width: 980px;
                    margin: 40px auto;
                    /*overflow: hidden;*/
                }

                #slider {
                    width: 940px;
                    height: 350px;
                    overflow: hidden;
                    border: 2px solid #FF9F1C;
                    position: relative;
                    padding: 3px;
                    float: left;
                    border-radius: 5px;
                }

                #slider img {
                    width: 940px;
                    height: 350px;
                }

                .slide-copy {
                    position: absolute;
                    left: 0;
                    bottom: 0;
                    padding: 5px 20px;
                    background-color: #FF9F1C;
                    opacity: .7;
                }

                #prev,
                #next {
                    position: relative;
                    float: left;
                    z-index: 111;
                    margin-top: 130px;
                    cursor: pointer;
                }

                #next {
                    margin-left: -45px;
                }

                #prev {
                    margin-right: -45px;

}

jquery代码

                    $(function () {

                    var speed = 500,
                        autoswitch = true,
                        autoswitch_speed = 4000;

                    $("#container").find('.slide').first().addClass('active');

                    $("#container").find('.slide').hide();

                    $('.active').show();

                    $("#next").on('click', next_slide)

                    $("#prev").on('click', prev_slide)



                    if (autoswitch == true) {
                        setInterval(function () {
                            next_slide();
                        }, autoswitch_speed);
                    }

                    //Switch to next slide
                    function next_slide() {
                        $(".active").removeClass("active").addClass("oldActive");
                        if ($(".oldActive").is(":last-child")) {
                            $(".slide").first().addClass("active");
                        } else {
                            $(".oldActive").next().addClass("active");
                        }
                        $(".oldActive").removeClass("oldActive");
                        $(".slide").fadeOut(speed);
                        $(".active").fadeIn(speed);
                    }

                    //Switch to pevious slide
                    function prev_slide() {
                        $(".active").removeClass("active").addClass("oldActive");
                        if ($(".oldActive").is(":first-child")) {
                            $(".slide").last().addClass("active");
                        } else {
                            $(".oldActive").prev().addClass("active");
                        }
                        $(".oldActive").removeClass("oldActive");
                        $(".slide").fadeOut(speed);
                        $(".active").fadeIn(speed);
                    }

});

1 个答案:

答案 0 :(得分:2)

试试这种方式。

&#13;
&#13;
jQuery(document).ready(function ($) {

  $('#checkbox').change(function(){
    setInterval(function () {
        moveRight();
    }, 3000);
  });
  
	var slideCount = $('#slider ul li').length;
	var slideWidth = $('#slider ul li').width();
	var slideHeight = $('#slider ul li').height();
	var sliderUlWidth = slideCount * slideWidth;
	
	$('#slider').css({ width: slideWidth, height: slideHeight });
	
	$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
	
    $('#slider ul li:last-child').prependTo('#slider ul');

    function moveLeft() {
        $('#slider ul').animate({
            left: + slideWidth
        }, 200, function () {
            $('#slider ul li:last-child').prependTo('#slider ul');
            $('#slider ul').css('left', '');
        });
    };

    function moveRight() {
        $('#slider ul').animate({
            left: - slideWidth
        }, 200, function () {
            $('#slider ul li:first-child').appendTo('#slider ul');
            $('#slider ul').css('left', '');
        });
    };

    $('a.control_prev').click(function () {
        moveLeft();
    });

    $('a.control_next').click(function () {
        moveRight();
    });

});    
&#13;
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600);	

html {
  border-top: 5px solid #fff;
  background: #58DDAF;
  color: #2a2a2a;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans';
}

h1 {
  color: #fff;
  text-align: center;
  font-weight: 300;
}

#slider {
  position: relative;
  overflow: hidden;
  margin: 20px auto 0 auto;
  border-radius: 4px;
}

#slider ul {
  position: relative;
  margin: 0;
  padding: 0;
  height: 200px;
  list-style: none;
}

#slider ul li {
  position: relative;
  display: block;
  float: left;
  margin: 0;
  padding: 0;
  width: 500px;
  height: 300px;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}

a.control_prev, a.control_next {
  position: absolute;
  top: 40%;
  z-index: 999;
  display: block;
  padding: 4% 3%;
  width: auto;
  height: auto;
  background: #2a2a2a;
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  font-size: 18px;
  opacity: 0.8;
  cursor: pointer;
}

a.control_prev:hover, a.control_next:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}

a.control_prev {
  border-radius: 0 2px 2px 0;
}

a.control_next {
  right: 0;
  border-radius: 2px 0 0 2px;
}

.slider_option {
  position: relative;
  margin: 10px auto;
  width: 160px;
  font-size: 18px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Incredibly Basic Slider</h1>
<div id="slider">
  <a href="#" class="control_next">>></a>
  <a href="#" class="control_prev"><</a>
  <ul>
    <li>SLIDE 1</li>
    <li style="background: #aaa;">SLIDE 2</li>
    <li>SLIDE 3</li>
    <li style="background: #aaa;">SLIDE 4</li>
  </ul>  
</div>

<div class="slider_option">
  <input type="checkbox" id="checkbox">
  <label for="checkbox">Autoplay Slider</label>
</div>
&#13;
&#13;
&#13;