javascript - slider 导航 - 下一个 上一个

标签 javascript jquery html

我创建了一个 JavaScript slider ,它只能自动更改图像。如何添加与自动循环一起工作的上一个和下一个按钮,如正常的 slider 导航?

这是脚本:

    function slider(sel, intr , i){
        var _slider = this;
        this.ind = i;
        this.selector = sel;
        this.slide = [];
        this.slide_active = 0;
        this.amount;
        this.intr = intr;
        this.selector.children().each(function(i){
            _slider.slide[i] = $(this);
            $(this).hide();
        })
        this.run();
    }
    slider.prototype.run = function(){
        var _s = this;
        this.slide[this.slide_active].fadeIn();
        setTimeout(function(){
            _s.slide[_s.slide_active].fadeOut()
            _s.slide_active = (_s.slide_active + 1) % _s.slide.length;
            _s.run();
        }, this.intr);
        var count = this.slide.length;
    }   

    var slides = [];

    $('.slider').each(function(i){
        slides[i] = new slider($(this) , 5000, i);
    });

这是标记:

    <div class="slider">
        <img src="img/modal_slider.jpg" alt="modal_slider" width="782" height="529">
        <img src="img/modal_slider1.jpg" alt="modal_slider" width="782" height="529">
        <a class="slider_btn left" href="javascript:void(0)"></a>
        <a class="slider_btn right" href="javascript:void(0)"></a>
    </div>

CSS:

.slider img{position:absolute};

这是它现在如何工作的 fiddle :http://jsfiddle.net/barney/vbRLU/ (归功于Barney)

最佳答案

我用一些新功能调整了您的 fiddle ,以允许使用两个按钮进行导航。 我希望这就是您所期待的。

更新了嵌入式导航按钮

WORKING EXAMPLE

<div class="small_box top_right slider">
    <img class="fittobox" src="http://www.lorempixel.com/854/592" alt="home10" />
    <img class="fittobox" src="http://www.lorempixel.com/435/392/sports" alt="home3" />
    <img class="fittobox" src="http://www.lorempixel.com/435/392/food" alt="home4" />
</div>

<style>
.slider img{
    display:none;
}
.fittobox {
    width:400px;
}
.next-arrow {
    right:10px;
}
.previous-arrow {
    left:10px;
}
.arrow {
    position:absolute;
    top:50%;
    right:10px;
    height:50px;
    width:50px;
    background-color:black;
    border-radius:10px;
    opacity:0.8;
    color:white;
    line-height:50px;
    text-align:center;
    font-size:10px;
    cursor:pointer;
}
</style>

function slider(sel, intr, i) {
    var _slider = this;
    this.ind = i;
    this.selector = sel;
    this.slide = [];
    this.slide_active = 0;
    this.amount;
    this.timer = null;
    this.selector.children('img').each(function (i) {
        _slider.slide[i] = $(this);
        $(this).hide();
    });

    //Display buttons and register events
    $(this.selector).hover(
    function () {
        $(this).append('<div id="previous-slider-' + i + '" class="previous-arrow arrow">Previous</div>');
        $(this).append('<div id="next-slider-' + i + '" class="next-arrow arrow">Next</div>');
        $('#next-slider-' + i).click(function () {
            _slider.next();
        });
        $('#previous-slider-' + i).click(function () {
            _slider.previous();
        });
    },
    function () {
        //Remove buttons and events
        $('.arrow').remove();
    });

    this.run();
}
slider.prototype.run = function () {
    this.next();
}
slider.prototype.next = function () {
    var _s = this;
    clearInterval(this.timer);
    _s.show(1);
    this.timer = setInterval(function () {
        _s.show(1);
    }, interval);
}
slider.prototype.previous = function () {
    var _s = this;
    clearInterval(this.timer);
    _s.show(-1);
    this.timer = setInterval(function () {
        _s.show(1);
    }, interval);
}
slider.prototype.show = function (shift) {
    var _s = this;
    _s.slide[_s.slide_active].fadeOut(300, function () {
        _s.slide_active = (_s.slide_active + shift < 0) ? _s.slide.length - 1 : (_s.slide_active + shift) % _s.slide.length;
        _s.slide[_s.slide_active].fadeIn(300);
    });
}

var slides = [];
var interval = 3000
$('.slider').each(function (i) {
    slides[i] = new slider($(this), interval, i);
});

关于javascript - slider 导航 - 下一个 上一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16064753/

相关文章:

javascript - Grunt/Gulp 源文件目标用途

javascript - Wordpress 缺少 css 和 javascript 文件

javascript - 将持久过滤器应用于表排序器表

html - 为什么 bootstrap 在我的 github 网页上不起作用?

javascript - 如何通过比较对象中具有不同元素的两个对象数组来过滤数组?

javascript - jQuery ajax 与 ES6 Promise

javascript - 匹配带空格和不带空格的模式

jquery - 从当前 url 添加/删除 anchor 名称而不刷新

javascript - 在 DOM 元素内部选择

php - 抛出名为 : "Unable to stream pdf: headers already sent" after all whitespaces removed 的异常