创建一个触摸滑块

时间:2014-02-27 11:51:14

标签: jquery css touch

我正试图让我的滑块在触摸屏上工作。用户必须能够滑动图像。我需要它在平板电脑和手机上工作。我试图使用$(“。portfolio-overlay”)。刷卡但是没有用,我不知道我做错了什么。

HTML

<div class="slider-wrapper">
<div class="slider">
    <div class="portfolio-overlay">
        <ul class="images">
            <li>
                <img width="704px" class="portfolio-active portfolio-single" src="http://s25.postimg.org/keaisiflb/mini_brown_fairy.jpg">
            </li>
            <li>
                <img width="704px" class="portfolio-inactive portfolio-single" src="http://s25.postimg.org/xwhf4srqn/mini_blue_fairy.jpg">
            </li>
            <li>
                <img width="704px" class="portfolio-inactive portfolio-single" src="http://s25.postimg.org/smcgdi7hr/mini_purple_fairy.jpg">
            </li>
        </ul>
    </div>
</div>
<ul class="triggers">
    <li>
        <img src="images/border-box.png">
    </li>
    <li>
        <img src="images/border-box.png">
    </li>
    <li>
        <img src="images/border-box.png">
    </li>
    <li>
        <img src="images/border-box.png">
    </li>
    <li>
        <img src="images/border-box.png">
    </li>
</ul>

CSS

.slider {
float: left;
height: 465px;
margin-left: auto;
margin-right: auto;
margin-top: 0;
overflow: hidden;
position: absolute;
width: 704px;
}
.images {
position:relative;
top:0px;
left:0px;
height:2660px;
}
.images li {
float:left;
position:relative;
top:0px;
left:0px;
}
.triggers {
left: 31.2%;
position: absolute;
top: 76.3%;
}
.triggers li {
float: left;
list-style: none outside none;
}
.triggers .selected {
background: url("../images/white-box.png") no-repeat scroll 0 3px rgba(0, 0, 0, 0);
}
.swipe-for-more {
left: 24.3%;
position: absolute;
top: 76.3%;
}
.control {
position:absolute;
top: 380px;
color:#fff;
cursor:pointer;
}

jquery的

    var triggers = $('.triggers li');
var images = $('.images li');
var lastElem = triggers.length - 1;
var slider = $('.slider .images');
var imgWidth = images.width();
var target;

triggers.first().addClass('selected');
slider.css('width', imgWidth * (lastElem + 1) + 'px');

function sliderResponse(target) {
    slider.stop(true, false).animate({
        'left': '-' + imgWidth * target + 'px'
    }, 300);
    triggers.removeClass('selected').eq(target).addClass('selected');
}

triggers.click(function () {
    if (!$(this).hasClass('selected')) {
        target = $(this).index();
        sliderResponse(target);
        resetTiming();
    }
});
$('.next').click(function () {
    target = $('.triggers .selected').index();
    target === lastElem ? target = 0 : target = target + 1;
    sliderResponse(target);
    resetTiming();
});
$('.prev').click(function () {
    target = $('.triggers .selected').index();
    lastElem = triggers.length - 1;
    target === 0 ? target = lastElem : target = target - 1;
    sliderResponse(target);
    resetTiming();
});

function sliderTiming() {
    target = $('.triggers .selected').index();
    target === lastElem ? target = 0 : target = target + 1;
    sliderResponse(target);
}
var timingRun = setInterval(function () {
    sliderTiming();
}, 5000);

function resetTiming() {
    clearInterval(timingRun);
    timingRun = setInterval(function () {
        sliderTiming();
    }, 5000);
}

$(".portfolio-overlay").swipe({

    swipe: function (event, direction, distance, duration, fingerCount) {
        if ($(".portfolio-single").length < 1) return false;
        clearTimeout(timeout);
        directionVAR = direction;
        if (direction == "left") {
            var bannerMove = $(".portfolio-active").index() + 1
            if ($(".portfolio-active").index() == ($(".portfolio-single").length - 1)) {

                bannerMove = 0;
            }
            rotateBanners(bannerMove);
        } else if (direction == "right") {
            rotateBanners(($(".portfolio-active").index() - 1));
        }
    },
    threshold: 0

});

这是我的jsfiddle:JSFiddle

1 个答案:

答案 0 :(得分:0)

可能或可能不是您需要的,但我使用iScroll进行触摸滑动。它可以设置为垂直或水平滚动,对齐点等。它非常灵活且可定制。易于设置。

http://cubiq.org/iscroll-4

http://iscrolljs.com