Javascript响应函数

时间:2017-09-19 07:58:05

标签: javascript responsive jssor

我正在建立一个网站,但我的功能有问题,无法制作响应滑块。

我尝试了很多东西,但我不知道如何调整滑块高度,最多占据屏幕尺寸的80%。

调整滑块大小的代码:

var MAX_WIDTH = 3000;

function ScaleSlider() {
  var containerElement = jssor_1_slider.$Elmt.parentNode;
  var containerWidth = containerElement.clientWidth;

  if (containerWidth) {

    var expectedWidth = Math.min(MAX_WIDTH || containerWidth, containerWidth);

    jssor_1_slider.$ScaleWidth(expectedWidth);
  } else {
    window.setTimeout(ScaleSlider, 30);
  }
}

ScaleSlider();

$Jssor$.$AddEvent(window, "load", ScaleSlider);
$Jssor$.$AddEvent(window, "resize", ScaleSlider);
$Jssor$.$AddEvent(window, "orientationchange", ScaleSlider);

1 个答案:

答案 0 :(得分:0)

请下载jssor slider开发包,找到demos/full-window-for-pc.html演示。

打开文件,替换

<div style="position:relative;top:0;left:0;width:100%;height:100%;overflow:hidden;">

<div style="position:relative;top:0;left:0;width:100%;height:80%;overflow:hidden;">

<!-- #region Jssor Slider Begin -->
<!-- Generator: Jssor Slider Maker -->
<!-- Source: https://www.jssor.com -->
<script src="../js/jssor.slider.min.js" type="text/javascript"></script>
<script type="text/javascript">
    jssor_1_slider_init = function() {

        var jssor_1_options = {
          $AutoPlay: 0,
          $Idle: 2000,
          $SlideEasing: $Jease$.$InOutSine,
          $DragOrientation: 3,
          $ArrowNavigatorOptions: {
            $Class: $JssorArrowNavigator$
          },
          $BulletNavigatorOptions: {
            $Class: $JssorBulletNavigator$
          }
        };

        var jssor_1_slider = new $JssorSlider$("jssor_1", jssor_1_options);

        //make sure to clear margin of the slider container element
        jssor_1_slider.$Elmt.style.margin = "";

        /*#region responsive code begin*/

        /*
            parameters to scale jssor slider to fill a container

            MAX_WIDTH
                prevent slider from scaling too wide
            MAX_HEIGHT
                prevent slider from scaling too high, default value is original height
            MAX_BLEEDING
                prevent slider from bleeding outside too much, default value is 1
                0: contain mode, allow up to 0% to bleed outside, the slider will be all inside container
                1: cover mode, allow up to 100% to bleed outside, the slider will cover full area of container
                0.1: flex mode, allow up to 10% to bleed outside, this is better way to make full window slider, especially for mobile devices
        */

        var MAX_WIDTH = 3000;
        var MAX_HEIGHT = 3000;
        var MAX_BLEEDING = 1;

        function ScaleSlider() {
            var containerElement = jssor_1_slider.$Elmt.parentNode;
            var containerWidth = containerElement.clientWidth;

            if (containerWidth) {
                var originalWidth = jssor_1_slider.$OriginalWidth();
                var originalHeight = jssor_1_slider.$OriginalHeight();

                var containerHeight = containerElement.clientHeight || originalHeight;

                var expectedWidth = Math.min(MAX_WIDTH || containerWidth, containerWidth);
                var expectedHeight = Math.min(MAX_HEIGHT || containerHeight, containerHeight);

                //constrain bullets, arrows inside slider area, it's optional, remove it if not necessary
                //if (MAX_BLEEDING >= 0 && MAX_BLEEDING < 1) {
                //    var widthRatio = expectedWidth / originalWidth;
                //    var heightRatio = expectedHeight / originalHeight;
                //    var maxScaleRatio = Math.max(widthRatio, heightRatio);
                //    var minScaleRatio = Math.min(widthRatio, heightRatio);

                //    maxScaleRatio = Math.min(maxScaleRatio / minScaleRatio, 1 / (1 - MAX_BLEEDING)) * minScaleRatio;
                //    expectedWidth = Math.min(expectedWidth, originalWidth * maxScaleRatio);
                //    expectedHeight = Math.min(expectedHeight, originalHeight * maxScaleRatio);
                //}

                //scale the slider to expected size
                jssor_1_slider.$ScaleSize(expectedWidth, expectedHeight, MAX_BLEEDING);

                //position slider at center in vertical orientation
                jssor_1_slider.$Elmt.style.top = ((containerHeight - expectedHeight) / 2) + "px";

                //position slider at center in horizontal orientation
                jssor_1_slider.$Elmt.style.left = ((containerWidth - expectedWidth) / 2) + "px";
            }
            else {
                window.setTimeout(ScaleSlider, 30);
            }
        }

        /*ios disable scrolling and bounce effect*/
        //$Jssor$.$AddEvent(document, "touchmove", function(event){event.touches.length < 2 && $Jssor$.$CancelEvent(event);});

        ScaleSlider();

        $Jssor$.$AddEvent(window, "load", ScaleSlider);
        $Jssor$.$AddEvent(window, "resize", ScaleSlider);
        $Jssor$.$AddEvent(window, "orientationchange", ScaleSlider);
        /*#endregion responsive code end*/
    };
</script>
<style>
    html, body {
        position:absolute;
        margin: 0;
        padding: 0;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }


    /* jssor slider loading skin spin css */
    .jssorl-009-spin img {
        animation-name: jssorl-009-spin;
        animation-duration: 1.6s;
        animation-iteration-count: infinite;
        animation-timing-function: linear;
    }

    @keyframes jssorl-009-spin {
        from {
            transform: rotate(0deg);
        }

        to {
            transform: rotate(360deg);
        }
    }


    .jssorb064 {position:absolute;}
    .jssorb064 .i {position:absolute;cursor:pointer;}
    .jssorb064 .i .b {fill:#000;fill-opacity:.5;stroke:#fff;stroke-width:400;stroke-miterlimit:10;stroke-opacity:0.5;}
    .jssorb064 .i:hover .b {fill-opacity:.8;}
    .jssorb064 .iav .b {fill:#ffe200;fill-opacity:1;stroke:#ffaa00;stroke-opacity:.7;stroke-width:2000;}
    .jssorb064 .iav:hover .b {fill-opacity:.6;}
    .jssorb064 .i.idn {opacity:.3;}

    .jssora051 {display:block;position:absolute;cursor:pointer;}
    .jssora051 .a {fill:none;stroke:#fff;stroke-width:360;stroke-miterlimit:10;}
    .jssora051:hover {opacity:.8;}
    .jssora051.jssora051dn {opacity:.5;}
    .jssora051.jssora051ds {opacity:.3;pointer-events:none;}
</style>
<div style="position:relative;top:0;left:0;width:100%;height:80%;overflow:hidden;">
    <div id="jssor_1" style="position:relative;margin:0 auto;top:0px;left:0px;width:960px;height:640px;overflow:hidden;visibility:hidden;">
        <!-- Loading Screen -->
        <div data-u="loading" class="jssorl-009-spin" style="position:absolute;top:0px;left:0px;width:100%;height:100%;text-align:center;background-color:rgba(0,0,0,0.7);">
            <img style="margin-top:-19px;position:relative;top:50%;width:38px;height:38px;" src="../svg/loading/static-svg/spin.svg" />
        </div>
        <div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:960px;height:640px;overflow:hidden;">
            <div>
                <img data-u="image" src="../img/gallery/960x640/001.jpg" />
                <div style="position:absolute;top:195px;left:330px;width:300px;height:250px;z-index:0;background-color:rgba(255,188,5,0.8);font-size:16px;color:#ffffff;line-height:30px;text-align:left;padding:10px;box-sizing:border-box;">Layout Settings <br /><br />
                    Slider Size: 960 x 640<br /> 
                    Scale To: Full Window (Flex Mode) <br />
                    Bleeding: 100% <br />
                    Max Width: 3000<br />
                    Max Height: 3000
                </div>
            </div>
            <div>
                <img data-u="image" src="../img/gallery/960x640/002.jpg" />
            </div>
            <div>
                <img data-u="image" src="../img/gallery/960x640/003.jpg" />
            </div>
            <div>
                <img data-u="image" src="../img/gallery/960x640/004.jpg" />
            </div>
            <div>
                <img data-u="image" src="../img/gallery/960x640/005.jpg" />
            </div>
            <div>
                <img data-u="image" src="../img/gallery/960x640/006.jpg" />
            </div>
            <div>
                <img data-u="image" src="../img/gallery/960x640/007.jpg" />
            </div>
            <div>
                <img data-u="image" src="../img/gallery/960x640/008.jpg" />
            </div>
            <div style="background-color:#ff7c28;">
                <div style="position:absolute;top:50px;left:50px;width:450px;height:62px;z-index:0;font-size:16px;color:#000000;line-height:24px;text-align:left;padding:5px;box-sizing:border-box;">Photos in this slider are to demostrate jssor slider,<br />
                    which are not licensed for any other purpose.
                </div>
            </div>
            <a data-u="any" href="https://www.jssor.com" style="display:none">carousel html</a>
        </div>
        <!-- Bullet Navigator -->
        <div data-u="navigator" class="jssorb064" style="position:absolute;bottom:12px;right:12px;" data-autocenter="1" data-scale="0.5" data-scale-bottom="0.75">
            <div data-u="prototype" class="i" style="width:16px;height:16px;">
                <svg viewBox="0 0 16000 16000" style="position:absolute;top:0;left:0;width:100%;height:100%;">
                    <circle class="b" cx="8000" cy="8000" r="5800"></circle>
                </svg>
            </div>
        </div>
        <!-- Arrow Navigator -->
        <div data-u="arrowleft" class="jssora051" style="width:55px;height:55px;top:0px;left:25px;" data-autocenter="2" data-scale="0.75" data-scale-left="0.75">
            <svg viewBox="0 0 16000 16000" style="position:absolute;top:0;left:0;width:100%;height:100%;">
                <polyline class="a" points="11040,1920 4960,8000 11040,14080 "></polyline>
            </svg>
        </div>
        <div data-u="arrowright" class="jssora051" style="width:55px;height:55px;top:0px;right:25px;" data-autocenter="2" data-scale="0.75" data-scale-right="0.75">
            <svg viewBox="0 0 16000 16000" style="position:absolute;top:0;left:0;width:100%;height:100%;">
                <polyline class="a" points="4960,1920 11040,8000 4960,14080 "></polyline>
            </svg>
        </div>
    </div>
</div>
<script type="text/javascript">jssor_1_slider_init();</script>
<!-- #endregion Jssor Slider End -->