【问题标题】:Horizontal scroll only if necessary仅在必要时水平滚动
【发布时间】:2014-12-04 06:43:43
【问题描述】:

我有一个水平滚动页面,其中箭头指示滚动。我正在使用以下代码,它工作正常。

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 条件以仅在水平溢出时显示箭头?

【问题讨论】:

  • 试图将它添加到我在 css 中的容器中,但没有帮助。或者你的意思是什么?
  • This 链接失效

标签: javascript html css scroll horizontal-scrolling


【解决方案1】:

你的 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;
        }

    }());

【讨论】:

  • 谢谢。抱歉,但我不喜欢 JS - 只有 HTML/CSS。我将它放在现有 JS 代码的上方和下方,但没有成功...
  • 所以你需要全面实施
  • 那么请接受这个答案...它也会帮助其他人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多