简体   繁体   English

流体宽度滑块

[英]Fluid Width Slider

I am making a website here: argit.bounde.co.uk 我在这里制作网站: argit.bounde.co.uk

I have followed a tutorial on making a slider and it works on the principle of having a ul of a big width and then the li's floated. 我遵循了有关制作滑块的教程,并且该教程的原理是先使ul变大,然后使li's浮动。 This is then all concealed by a div which has overflow: hidden . 然后,所有这些都由一个已overflow: hiddendiv overflow: hidden Then have jquery which animates the margin-left = imgWidth 然后有jquery动画margin-left = imgWidth

The rest of my site is fluid width and so obviously I need the slider to follow along other wise it looks a bit silly. 我网站的其余部分是流体宽度,因此很明显,我需要滑块沿其他方向跟随,看起来有点傻。 At the moment I have given the li's a set width with jquery and then have img's as 100%. 目前,我已经给li's设置了jquery的宽度,然后将img设置为100%。 This solution is okay, but falls apart when the device doesn't support jQuery so I would prefer a more robust method. 这种解决方案是可以的,但是当设备不支持jQuery时会崩溃,所以我希望使用更可靠的方法。 I cant set the ul to 100% as then the images no longer are in a line and they simple stack underneath each other 我无法将ul设置为100%,因为图像不再排成一行,它们彼此简单堆叠

Can anyone think of a way I could achieve this? 谁能想到我可以实现这一目标的方法?

Full slider jQuery here: 完整的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
})
};

I've linked to a fiddle of a plugin I programmed that does this. 我已经链接到我为此编写的插件的小提琴。 The requirements are that the parent container has position: relative, and to a lesser extent overflow: hidden and that it is called after images are loaded. 要求父容器具有位置:相对,并且在较小程度上具有溢出:隐藏,并且在加载图像后调用它。 Feel free to take the code and learn from it. 随意获取代码并从中学习。

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

JS 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' : ''
                });
            }
        }
    });
};

To explain the logic, all it does is sets the width to 100% and checks if it's height is greater, if it isn't then width 100% is fine. 为了解释逻辑,它所做的只是将宽度设置为100%,并检查它的高度是否更大,如果不是,那么宽度100%就可以了。 If not it switches it to height 100%. 如果不是,则将其切换为高度100%。 On images if one size attribute is set then the other one resizes accordingly. 在图像上,如果设置了一个大小属性,则另一个将相应地调整大小。 After the size logic is complete. 大小逻辑完成之后。 it just centers the image using position absolute and margining. 它只使用绝对位置和边距位置将图像居中。

You can detect does device support jQuery: 您可以检测到设备是否支持jQuery:

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

And then use specific css styles for this class: 然后为此类使用特定的CSS样式:

.jQuery li {
    /* some style */ 
}

If class 'jQuery' setted then device support jQuery. 如果设置了“ jQuery”类,则设备支持jQuery。

The simplest method would be to not float the your panels (li's) and set them to be 100% width of the wrapping div. 最简单的方法是不浮动面板(li's)并将其设置为环绕div的100%宽度。 Then when the user navigates, position the next panel out of view to the right at 100% margin, then animate it to 0 margin while you animate the previous panel to -100% margin. 然后,当用户导航时,将下一个面板以100%的边距向右移出视图,然后将其动画化为0边距,同时将前一个面板动画化为-100%边距。 Do the opposite when the user selects the previous panel. 当用户选择上一个面板时,请执行相反的操作。 I don't think this method works well in older IE, but I could be mistaken. 我认为这种方法在较旧的IE中效果不佳,但我可能会误会。

Alternatively, you can bind to the window resize event (it would be best to throttle the event) and resize the panels and any saved widths/heights 或者,您可以绑定到窗口调整大小事件(最好限制事件),并调整面板大小和任何保存的宽度/高度

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

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