繁体   English   中英

仅在必要时水平滚动

[英]Horizontal scroll only if necessary

我有一个水平滚动页面,其中指示箭头滚动。 我正在使用下面的代码,效果很好。

HTML:

<div id="container">
<div id="parent">
    <div class="contentBlock">1</div>
    <div class="contentBlock">2</div>
    <div class="contentBlock">3</div>
    <div class="contentBlock">4</div>
    <div class="contentBlock">5</div>
</div>
<span id="panLeft" class="panner" data-scroll-modifier='-1'>Left</span>
<span id="panRight" class="panner" data-scroll-modifier='1'>Right</span>

CSS:

#container{
    width:600px;
    overflow-x:hidden;
}

#parent {
    width:6000px;
}
.contentBlock {
    font-size:10em;
    text-align:center;
    line-height:400px;
    height:400px;
    width:500px;
    margin:10px;
    border:1px solid black;
    float:left;
}
.panner {
    border:1px solid black;
    display:block;
    position:fixed;
    width:50px;
    height:50px;
    top:45%;
}
.active {
    color:red;
}
#panLeft {
    left:0px;
}
#panRight {
    right:0px;
}

使用Javascript:

(function () {

    var scrollHandle = 0,
        scrollStep = 5,
        parent = $("#container");

    //Start the scrolling process
    $(".panner").on("mouseenter", function () {
        var data = $(this).data('scrollModifier'),
            direction = parseInt(data, 10);

        $(this).addClass('active');

        startScrolling(direction, scrollStep);
    });

    //Kill the scrolling
    $(".panner").on("mouseleave", function () {
        stopScrolling();
        $(this).removeClass('active');
    });

    //Actual handling of the scrolling
    function startScrolling(modifier, step) {
        if (scrollHandle === 0) {
            scrollHandle = setInterval(function () {
                var newOffset = parent.scrollLeft() + (scrollStep * modifier);

                parent.scrollLeft(newOffset);
            }, 10);
        }
    }

    function stopScrolling() {
        clearInterval(scrollHandle);
        scrollHandle = 0;
    }

}());

您也可以在此处的WordPress安装中查看代码: http : //ustria-steila.ch/test

箭头和滚动条非常好-但是我在不同的站点上放置了不同数量的文本和图像。 因此,有些页面需要水平滚动,而有些则不需要。 如何添加某种if条件以仅在水平溢出时显示箭头?

您的JavaScript代码应如下所示:

(function () {

        var scrollHandle = 0,
            scrollStep = 5,
            parent = $("#container");
        if(checkOverflow()){
            $(".panner").show();
        }
        else
            $(".panner").hide();
        //Start the scrolling process
        $(".panner").on("mouseenter", function () {
            var data = $(this).data('scrollModifier'),
                direction = parseInt(data, 10);

            $(this).addClass('active');

            startScrolling(direction, scrollStep);
        });

        //Kill the scrolling
        $(".panner").on("mouseleave", function () {
            stopScrolling();
            $(this).removeClass('active');
        });

        //Actual handling of the scrolling
        function startScrolling(modifier, step) {
            if (scrollHandle === 0) {
                scrollHandle = setInterval(function () {
                    var newOffset = parent.scrollLeft() + (scrollStep * modifier);

                    parent.scrollLeft(newOffset);
                }, 10);
            }
        }

        function stopScrolling() {
            clearInterval(scrollHandle);
            scrollHandle = 0;
        }
         function checkOverflow()
        {
            var el=document.getElementById('container');
           var curOverflow = el.style.overflowX;
           if ( !curOverflow || curOverflow === "visible" )
              el.style.overflowX = "hidden";

           var isOverflowing = el.clientWidth < el.scrollWidth; 
            el.style.overflowX = curOverflow;

           return isOverflowing;
        }

    }());

暂无
暂无

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

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