繁体   English   中英

流体宽度滑块

[英]Fluid Width Slider

我在这里制作网站: argit.bounde.co.uk

我遵循了有关制作滑块的教程,并且该教程的原理是先使ul变大,然后使li's浮动。 然后,所有这些都由一个已overflow: hiddendiv overflow: hidden 然后有jquery动画margin-left = imgWidth

我网站的其余部分是流体宽度,因此很明显,我需要滑块沿其他方向跟随,看起来有点傻。 目前,我已经给li's设置了jquery的宽度,然后将img设置为100%。 这种解决方案是可以的,但是当设备不支持jQuery时会崩溃,所以我希望使用更可靠的方法。 我无法将ul设置为100%,因为图像不再排成一行,它们彼此简单堆叠

谁能想到我可以实现这一目标的方法?

完整的jQuery jQuery在这里:

var current = 1;

function slider () {
var sliderUl = $('div#slider ul'),
    imgs = sliderUl.find('img'),
    imgWidth = imgs.width(), 
    imgLength = imgs.length, 
    totalImgsWidth = imgLength * imgWidth; 

var direction = $(this).data('dir'),
    loc = imgWidth;

if( direction == 'next') {
    ++current;
} else {
    --current;
}

if( current === 0) {
    current = imgLength;
    loc = totalImgsWidth - imgWidth;
    direction = 'next';
} else if ( current - 1 === imgLength ) {
    current = 1;
    loc = 0;
}


transition(sliderUl, loc, direction);
};

function transition( container, loc, direction ) {
var ease;
var unit;

if (direction && loc !== 0) {
    unit = (direction === 'next') ? '-=' : '+=';
}

container.animate({
    'margin-left': unit ? (unit + loc) : loc
})
};

我已经链接到我为此编写的插件的小提琴。 要求父容器具有位置:相对,并且在较小程度上具有溢出:隐藏,并且在加载图像后调用它。 随意获取代码并从中学习。

http://jsfiddle.net/dhQk/35K8X/7/show/

JS

$.fn.fitToParent = function (type, align) {
    type = typeof type === 'undefined' ? 'fit' : type;
    align = typeof align === 'undefined' ? 'center' : align;
    return this.each(function () {
        var $this = $(this);
        var $parent = $(this).parent();
        if ($this.is('img')) {
            $this.css({
                'position': 'absolute',
                    'width': '100%',
                    'height': 'auto'
            }).css({ //Allow Height to adjust
                'left': '',
                    'margin-left': '',
                    'top': align == 'center' ? '50%' : (align == 'left' ? '0px' : ''),
                    'bottom': '',
                    'margin-top': align == 'center' ? (-$this.height() / 2) + 'px' : ''
            });
            //Size by height depending on sizing type
            if (($this.height() > $parent.height() && type === 'fit') || ($this.height() < $parent.height() && type === 'fill')) {
                $this.css({
                    'width': 'auto',
                        'height': '100%'
                }).css({ //Allow Width to adjust
                    'top': '',
                        'margin-top': '',
                        'left': align == 'center' ? '50%' : (align == 'left' ? '0px' : ''),
                        'right': align == 'right' ? '0px' : '',
                        'margin-left': align == 'center' ? (-$this.width() / 2) + 'px' : ''
                });
            }
            if (type === 'none') {
                $this.css({
                    'width': '',
                        'height': ''
                }).css({ //Allow Width to adjust
                    'top': '50%',
                        'bottom': '',
                        'margin-top': (-$this.height() / 2) + 'px',
                        'left': align == 'center' ? '50%' : (align == 'left' ? '0px' : ''),
                        'right': align == 'right' ? '0px' : '',
                        'margin-left': align == 'center' ? (-$this.width() / 2) + 'px' : ''
                });
            }
        }
    });
};

为了解释逻辑,它所做的只是将宽度设置为100%,并检查它的高度是否更大,如果不是,那么宽度100%就可以了。 如果不是,则将其切换为高度100%。 在图像上,如果设置了一个大小属性,则另一个将相应地调整大小。 大小逻辑完成之后。 它只使用绝对位置和边距位置将图像居中。

您可以检测到设备是否支持jQuery:

$('html').addClass('jQuery');

然后为此类使用特定的CSS样式:

.jQuery li {
    /* some style */ 
}

如果设置了“ jQuery”类,则设备支持jQuery。

最简单的方法是不浮动面板(li's)并将其设置为环绕div的100%宽度。 然后,当用户导航时,将下一个面板以100%的边距向右移出视图,然后将其动画化为0边距,同时将前一个面板动画化为-100%边距。 当用户选择上一个面板时,请执行相反的操作。 我认为这种方法在较旧的IE中效果不佳,但我可能会误会。

或者,您可以绑定到窗口调整大小事件(最好限制事件),并调整面板大小和任何保存的宽度/高度

暂无
暂无

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

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