我怎样才能让 JSSOR 改变它的纵横比? [英] How can I make JSSOR change its aspect ratio?

查看:18
本文介绍了我怎样才能让 JSSOR 改变它的纵横比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在业余时间是一名艺术摄影师,我正在使用一个很棒的开源幻灯片库为我的网站(注:NSFW)进行新设计,JSSOR.

我想在不改变照片纵横比的情况下填满浏览器(,将它们尽可能大地居中).

1$FillMode 可以很好地解决这个问题,但是我在创建代码来响应浏览器调整大小/旋转时遇到问题.

在调整大小时计算适当的新高度/宽度(浏览器减去页脚文本和一点边距)很容易,但似乎无法在幻灯片放映后以编程方式设置宽度初始化.我错过了什么吗?

我见过的所有 JSSOR 响应式代码(例如这个答案) 使用了 $ScaleWidth() 方法.不幸的是,该方法试图保持幻灯片画布的纵横比,而我希望幻灯片画布随着浏览器拉伸(同时保持>photo 通过 $FillMode=1 逻辑).

我研究了代码对于 $ScaleWidth(),但是里面有一些奇怪的 DOM-voodoo,所以我还没有想出如何创建一个我自己的方法来强制调整到特定的宽度 高度.

想法?

解决方案

脚本

<!-- 它适用于从 1.x 到 2.x 的所有 jquery 版本 --><script type="text/javascript" src="../js/jquery-1.9.1.min.js"></script><!-- 使用 jssor.slider.mini.js (39KB) 或 jssor.sliderc.mini.js (31KB,带标题,无幻灯片) 或 jssor.slider.mini.js (26KB,无标题,无幻灯片)而是为了发布 --><!-- jssor.slider.mini.js = jssor.sliderc.mini.js = jssor.slider.mini.js = (jssor.core.js + jssor.utils.js + jssor.slider.js) --><script type="text/javascript" src="../js/jssor.core.js"></script><script type="text/javascript" src="../js/jssor.utils.js"></script><script type="text/javascript" src="../js/jssor.slider.js"></script><脚本>jQuery(document).ready(function ($) {var _SlideshowTransitions = [//褪色{ $Duration: 700, $Opacity: 2, $Brother: { $Duration: 1000, $Opacity: 2 } }];变量选项 = {$FillMode: 1,//[可选] 幻灯片中图片的填充方式,0 拉伸,1 包含(保持纵横比并全部放入幻灯片内),2 覆盖(保持纵横比并覆盖整个幻灯片),4 实际大小,5 包含大图,小图实际大小,默认值为0$SlideDuration: 800,//[可选] 以毫秒为单位指定幻灯片的默认持续时间(滑动),默认值为500$DragOrientation: 3,//[可选] 拖动幻灯片的方向,0 不拖动,1 水平,2 垂直,3 两者之一,默认值为 1(注意 $DisplayPieces 较大时 $DragOrientation 应与 $PlayOrientation 相同大于1,或停车位置不为0)$AutoPlay: true,//[可选] 是否自动播放,开启幻灯片,该选项必须设置为true,默认值为false$AutoPlayInterval: 1500,//[可选] 如果滑块自动播放,则从上一张停止后到下一张幻灯片的间隔(以毫秒为单位),默认值为 3000$SlideshowOptions: {//[可选] 指定和启用幻灯片放映的选项$Class: $JssorSlideshowRunner$,//[Required] 创建幻灯片实例的类$Transitions: _SlideshowTransitions,//[Required] 用于播放幻灯片的幻灯片转换数组$TransitionsOrder: 1,//[Optional] 选择播放幻灯片的方式,1 Sequence,0 Random$ShowLink: true//[可选] 幻灯片运行时是否将幻灯片链接置于滑块顶部,默认值为false},$ArrowNavigatorOptions: {//[可选] 用于指定和启用箭头导航器的选项$Class: $JssorArrowNavigator$,//[Requried] 创建箭头导航器实例的类$ChanceToShow: 2,//[必需] 0 从不,1 鼠标悬停,2 始终$AutoCenter: 2,//[可选] 父容器自动居中箭头,0 否,1 水平,2 垂直,3 两者,默认值为 0$Steps: 1//【可选】每个导航请求的步数,默认值为1}};var jssor_slider1 = new $JssorSlider$("slider1_container", options);//响应代码开始//如果您不希望在窗口调整大小时滑块缩放,您可以删除响应代码函数 ScaleSlider() {var windowWidth = $(window).width();如果(窗口宽度){var windowHeight = $(window).height();var originalWidth = jssor_slider1.$OriginalWidth();var originalHeight = jssor_slider1.$OriginalHeight();var scaleWidth = windowWidth;if (originalWidth/windowWidth < originalHeight/windowHeight) {scaleWidth = Math.ceil(windowHeight/originalHeight * originalWidth);}jssor_slider1.$ScaleWidth(scaleWidth);}别的window.setTimeout(ScaleSlider, 30);}ScaleSlider();$(window).bind("load", ScaleSlider);$(window).bind("resize", ScaleSlider);$(window).bind("orientationchange", ScaleSlider);//响应代码结束});

html

<div style="position: relative; width: 100%; overflow: hidden;"><div style="position: relative; left: 50%; width: 5000px; text-align: center; margin-left: -2500px;"><!-- Jssor 滑块开始--><!-- 您可以将内联样式移动到 css 文件或 css 块.--><div id="slider1_container" style="position: relative; margin: 0 auto; top: 0px; left: 0px; width: 1000px; height: 1000px;"><!-- 幻灯片容器 --><div u="slides" style="cursor: move; position: relative; left: 0px; top: 0px; width: 1000px; height: 1000px; overflow: hidden; "><div><img u="image" src="tallent/web-2012-01-29-183109.jpg"/></div><div><img u="image" src="tallent/web-2014-04-01-104205.jpg"/></div><div><img u="image" src="tallent/test-wide.jpg"/></div><div><img u="image" src="tallent/web-2012-02-04-123703.jpg"/></div><div><img u="image" src="tallent/web-2012-06-02-083648.jpg"/></div><div><img u="image" src="tallent/web-2014-04-01-093103.jpg"/></div><div><img u="image" src="tallent/web-2013-11-20-131902.jpg"/></div><div><img u="image" src="tallent/web-2013-06-30-184152.jpg"/></div><div><img u="image" src="tallent/web-2013-04-19-163938.jpg"/></div><div><img u="image" src="tallent/web-2013-03-10-084125.jpg"/></div>

<!-- Arrow 导航器皮肤开始 --><风格>/* jssor 滑块箭头导航皮肤 03 css *//*.jssora03l(正常).jssora03r(正常).jssora03l:hover(正常鼠标悬停).jssora03r:hover(正常鼠标悬停).jssora03ldn(鼠标按下).jssora03rdn(鼠标按下)*/.jssora03l、.jssora03r、.jssora03ldn、.jssora03rdn {位置:绝对;光标:指针;显示:块;背景: url(../img/a03.png) 不重复;溢出:隐藏;}.jssora03l {背景位置:-3px -33px;}.jssora03r {背景位置:-63px -33px;}.jssora03l:悬停{背景位置:-123px -33px;}.jssora03r:悬停{背景位置:-183px -33px;}.jssora03ldn {背景位置:-243px -33px;}.jssora03rdn {背景位置:-303px -33px;}</风格><!-- 向左箭头 --><span u="arrowleft" class="jssora03l" style="width: 55px; height: 55px; top: 123px; left: 8px;" ></span><!-- 向右箭头--><span u="arrowright" class="jssora03r" style="width: 55px; height: 55px; top: 123px; right: 8px"></span><!-- Arrow Navigator Skin End -->

I'm an art photographer in my spare time, and I'm working on a new design for my web site (note: NSFW) using a great open-source slideshow library, JSSOR.

I want to fill the browser without changing the aspect ratio of the photos (i.e., center them as large as possible).

A $FillMode of 1 works beautifully for this, but I'm having trouble creating code to respond to browser resize/rotation.

Calculating the proper new height/width (browser minus footer text and a little margin) on resize is easy, but there appears to be no way to set the width and height programmatically after the slideshow has initialized. Am I missing something?

All of the responsive code I've seen for JSSOR (e.g., this answer) uses the $ScaleWidth() method. Unfortunately, that method attempts to maintain the aspect ratio of the slideshow canvas, while I want the slideshow canvas to stretch with the browser (while maintaining the aspect ration of the photo via the $FillMode=1 logic).

I dug into the code for $ScaleWidth(), but there's some weird DOM-voodoo in there, so I haven't figured out how to create a my own method to force-resize to a particular width and height.

Ideas?

解决方案

Scripts

<!-- it works the same with all jquery version from 1.x to 2.x -->
<script type="text/javascript" src="../js/jquery-1.9.1.min.js"></script>
<!-- use jssor.slider.mini.js (39KB) or jssor.sliderc.mini.js (31KB, with caption, no slideshow) or jssor.sliders.mini.js (26KB, no caption, no slideshow) instead for release -->
<!-- jssor.slider.mini.js = jssor.sliderc.mini.js = jssor.sliders.mini.js = (jssor.core.js + jssor.utils.js + jssor.slider.js) -->
<script type="text/javascript" src="../js/jssor.core.js"></script>
<script type="text/javascript" src="../js/jssor.utils.js"></script>
<script type="text/javascript" src="../js/jssor.slider.js"></script>
<script>
    jQuery(document).ready(function ($) {
        var _SlideshowTransitions = [
        //Fade
        { $Duration: 700, $Opacity: 2, $Brother: { $Duration: 1000, $Opacity: 2 } }
        ];

        var options = {
            $FillMode: 1,                                       //[Optional] The way to fill image in slide, 0 stretch, 1 contain (keep aspect ratio and put all inside slide), 2 cover (keep aspect ratio and cover whole slide), 4 actual size, 5 contain for large image, actual size for small image, default value is 0
            $SlideDuration: 800,                                //[Optional] Specifies default duration (swipe) for slide in milliseconds, default value is 500
            $DragOrientation: 3,                                //[Optional] Orientation to drag slide, 0 no drag, 1 horizental, 2 vertical, 3 either, default value is 1 (Note that the $DragOrientation should be the same as $PlayOrientation when $DisplayPieces is greater than 1, or parking position is not 0)
            $AutoPlay: true,                                    //[Optional] Whether to auto play, to enable slideshow, this option must be set to true, default value is false
            $AutoPlayInterval: 1500,                            //[Optional] Interval (in milliseconds) to go for next slide since the previous stopped if the slider is auto playing, default value is 3000
            $SlideshowOptions: {                                //[Optional] Options to specify and enable slideshow or not
                $Class: $JssorSlideshowRunner$,                 //[Required] Class to create instance of slideshow
                $Transitions: _SlideshowTransitions,            //[Required] An array of slideshow transitions to play slideshow
                $TransitionsOrder: 1,                           //[Optional] The way to choose transition to play slide, 1 Sequence, 0 Random
                $ShowLink: true                                    //[Optional] Whether to bring slide link on top of the slider when slideshow is running, default value is false
            },

            $ArrowNavigatorOptions: {                       //[Optional] Options to specify and enable arrow navigator or not
                $Class: $JssorArrowNavigator$,              //[Requried] Class to create arrow navigator instance
                $ChanceToShow: 2,                               //[Required] 0 Never, 1 Mouse Over, 2 Always
                $AutoCenter: 2,                                 //[Optional] Auto center arrows in parent container, 0 No, 1 Horizontal, 2 Vertical, 3 Both, default value is 0
                $Steps: 1                                       //[Optional] Steps to go for each navigation request, default value is 1
            }
        };

        var jssor_slider1 = new $JssorSlider$("slider1_container", options);

        //responsive code begin
        //you can remove responsive code if you don't want the slider scales while window resizes
        function ScaleSlider() {
            var windowWidth = $(window).width();

            if (windowWidth) {
                var windowHeight = $(window).height();
                var originalWidth = jssor_slider1.$OriginalWidth();
                var originalHeight = jssor_slider1.$OriginalHeight();

                var scaleWidth = windowWidth;
                if (originalWidth / windowWidth < originalHeight / windowHeight) {
                    scaleWidth = Math.ceil(windowHeight / originalHeight * originalWidth);
                }

                jssor_slider1.$ScaleWidth(scaleWidth);
            }
            else
                window.setTimeout(ScaleSlider, 30);
        }

        ScaleSlider();

        $(window).bind("load", ScaleSlider);
        $(window).bind("resize", ScaleSlider);
        $(window).bind("orientationchange", ScaleSlider);
        //responsive code end

    });
</script>

html

<div style="position: relative; width: 100%; overflow: hidden;">
    <div style="position: relative; left: 50%; width: 5000px; text-align: center; margin-left: -2500px;">
        <!-- Jssor Slider Begin -->
        <!-- You can move inline styles to css file or css block. -->
        <div id="slider1_container" style="position: relative; margin: 0 auto; top: 0px; left: 0px; width: 1000px; height: 1000px;">
            <!-- Slides Container -->
            <div u="slides" style="cursor: move; position: relative; left: 0px; top: 0px; width: 1000px; height: 1000px; overflow: hidden; ">
                <div><img u="image" src="tallent/web-2012-01-29-183109.jpg" /></div>
                <div><img u="image" src="tallent/web-2014-04-01-104205.jpg" /></div>
                <div><img u="image" src="tallent/test-wide.jpg" /></div>
                <div><img u="image" src="tallent/web-2012-02-04-123703.jpg" /></div>
                <div><img u="image" src="tallent/web-2012-06-02-083648.jpg" /></div>
                <div><img u="image" src="tallent/web-2014-04-01-093103.jpg" /></div>
                <div><img u="image" src="tallent/web-2013-11-20-131902.jpg" /></div>
                <div><img u="image" src="tallent/web-2013-06-30-184152.jpg" /></div>
                <div><img u="image" src="tallent/web-2013-04-19-163938.jpg" /></div>
                <div><img u="image" src="tallent/web-2013-03-10-084125.jpg" /></div>
            </div>

            <!-- Arrow Navigator Skin Begin -->
            <style>
                        /* jssor slider arrow navigator skin 03 css */
                        /*
                .jssora03l              (normal)
                .jssora03r              (normal)
                .jssora03l:hover        (normal mouseover)
                .jssora03r:hover        (normal mouseover)
                .jssora03ldn            (mousedown)
                .jssora03rdn            (mousedown)
                */
                        .jssora03l, .jssora03r, .jssora03ldn, .jssora03rdn {
                            position: absolute;
                            cursor: pointer;
                            display: block;
                            background: url(../img/a03.png) no-repeat;
                            overflow: hidden;
                        }

                        .jssora03l {
                            background-position: -3px -33px;
                        }

                        .jssora03r {
                            background-position: -63px -33px;
                        }

                        .jssora03l:hover {
                            background-position: -123px -33px;
                        }

                        .jssora03r:hover {
                            background-position: -183px -33px;
                        }

                        .jssora03ldn {
                            background-position: -243px -33px;
                        }

                        .jssora03rdn {
                            background-position: -303px -33px;
                        }
            </style>
            <!-- Arrow Left -->
            <span u="arrowleft" class="jssora03l" style="width: 55px; height: 55px; top: 123px; left: 8px;">
            </span>
            <!-- Arrow Right -->
            <span u="arrowright" class="jssora03r" style="width: 55px; height: 55px; top: 123px; right: 8px">
            </span>
            <!-- Arrow Navigator Skin End -->
        </div>
    </div>
</div>

这篇关于我怎样才能让 JSSOR 改变它的纵横比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆