简体   繁体   English

触摸滑块不起作用

[英]Touch slider not working

I'm trying to get my slider to work on a touch screen so I found a plugin called touchSwipt. 我试图让滑块在触摸屏上工作,所以我找到了一个名为touchSwipt的插件。 Here is the link to the demo of more or less how I would like it. 这是我想要的演示的链接。

DEMO 演示

and this is the site where I saw it 这是我看到它的地方

LINK TO SITE 链接到网站

It works if I just copy and paste the html of the demo, but when I try to add my own stuff it doesn't work. 如果我只复制并粘贴演示的html,它会起作用,但是当我尝试添加自己的东西时,它将无法工作。 Here is the JSFiddle. 这是JSFiddle。 JSFIDDLE JSFIDDLE

Here is my html 这是我的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>

Here is my css 这是我的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;
}

and this is my Jquery 这是我的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

});

Try to wrap your code inside DOM ready handler $(function() { }); 尝试将代码包装在DOM准备处理程序$(function() { }); to make sure all the Document Object Model (DOM) is ready for JavaScript code to execute. 确保所有文档对象模型(DOM)准备就绪,可以执行JavaScript代码。

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

or wrap your code inside $(window).load(function() { ... }) to make sure the entire page (images or iframes), not just the DOM, is ready 或将您的代码包装在$(window).load(function() { ... }) ,以确保整个页面(图像或iframe)(而不仅仅是DOM)已准备就绪

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM