触摸滑块不起作用

时间:2014-02-27 14:37:43

标签: jquery plugins

我正试图让我的滑块在触摸屏上工作,所以我找到了一个名为touchSwipt的插件。以下是我想要的或多或少的演示链接。

DEMO

这是我看到它的网站

LINK TO SITE

如果我只是复制并粘贴演示的html,它会起作用,但是当我尝试添加自己的东西时,它不起作用。 这是JSFiddle。 JSFIDDLE

这是我的HTML

               <div class="slider-wrapper">
                <div class="slider">
                    <div id="test" class="box">
                    <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>

                    </ul> 
                    </div>   
                </div>

                <ul class="triggers">
                    <li><img src="http://s25.postimage.org/hj22mxdgr/border_box.png">  </li>
                    <li><img src="http://s25.postimage.org/hj22mxdgr/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("http://s25.postimg.org/8cjrzn88b/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);
}

$("#test").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;
            }

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

});

1 个答案:

答案 0 :(得分:0)

尝试将代码包装在DOM ready handler $(function() { });中,以确保所有文档对象模型(DOM)都已准备好执行JavaScript代码。

$(function() {
    // Your code here
});

或将您的代码包装在$(window).load(function() { ... })中,以确保整个页面(图片或iframe),而不仅仅是DOM,已准备就绪

$(window).load(function() {
    // Your code here
});