如何将“ .position()。left”的值和“ scrollLeft”的行为应用于溢出容器

时间:2019-02-13 22:53:04

标签: jquery css html5 flexbox position

我正在尝试通过调用一个函数将溢出的flexbox容器的内容从一个div水平滚动到另一个div。

HTML #href和jQuery window.location完全满足了我的目的,但他们也将容器滚动到窗口顶部,而不是仅滚动容器的内容而不更改窗口位置。

我认为解决方案是:

var currentSlideFocus = $("#select_slide_" + currentSlide).position().left;
$("#slideWindow").scrollLeft(currentSlideFocus);

但是,currentSlideFocus的值仅约为我预期的10%,scrollLeft的滚动行为量似乎仅为currentSlideFocus的10%。有什么作用?

position()。left是否测量元素左侧相对于其容器左侧的距离,还是我误解了?

这是下面的框架项目的JSfiddle。谢谢。 here

<!DOCTYPE html> 
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
            var currentSlide = 1;
            var slideLink = 1;
            var totalSlides;
            var currentSlideFocus;
            var slideIntvl;
            var slideVW;
            jQuery(function() {
                totalSlides = $('.slide_frame').length;
                $(".current_slide").html(currentSlide);
                $(".total_slides").html(totalSlides);
                $(".position_left").html(currentSlideFocus);
                $(makeControlLinks);
                $("#select_slide_" + currentSlide).addClass('active_slide_control');
                $(slideSlider);
            })();
            /* slide every 5 seconds, pause on mouse hover, resume after mouseout */
            function slideSlider(){
                slideIntvl = setInterval(slideNext, 5000);
                $(".slider_wrapper").hover(function() {     
                    clearInterval(slideIntvl);
                }, function() { 
                    slideIntvl = setInterval(slideNext, 5000);
                });
            }
            /* previous and next controls */
            function slideNext(){
                if (currentSlide < totalSlides) {
                    nextSlide = currentSlide + 1;
                } else {
                    nextSlide = 1;
                }
                currentSlide = nextSlide;
                $(changeSlide);
            }
            function slidePrev(){
                if (currentSlide == 1) {
                    prevSlide = totalSlides
                } else {
                    prevSlide = currentSlide - 1;
                }
                currentSlide = prevSlide;
                $(changeSlide);
            }
            /* formula to change that slide when the time comes */
            function changeSlide(){
                var currentSlideFocus = $("#select_slide_" + currentSlide).position().left;
                $("#slideWindow").scrollLeft(currentSlideFocus);
                $(".active_slide_control").removeClass('active_slide_control');
                $("#select_slide_" + currentSlide).addClass('active_slide_control');
                $(".current_slide").html(currentSlide);
                $(".position_left").html(currentSlideFocus);
            }
            /* --------- generate skip-to links ---------- */
            function makeControlLinks(){
                if (slideLink <= totalSlides){
                    $("#frame_select").append('<a id="select_slide_'+slideLink+'" class="selector" onclick="selectSlide'+slideLink+'()">'+slideLink+'</a>');
                    slideLink = slideLink + 1;
                    $(makeControlLinks);
                } else {
                    return false;
                }   
            }
            /* individual jump-to specific controls */
            function selectSlide1(){
                currentSlide = 1;
                $(changeSlide);
            }
            function selectSlide2(){
                currentSlide = 2;
                $(changeSlide);
            }
            function selectSlide3(){
                currentSlide = 3;
                $(changeSlide);
            }
            function selectSlide4(){
                currentSlide = 4;
                $(changeSlide);
            }
            function selectSlide5(){
                currentSlide = 5;
                $(changeSlide);
            }
            function selectSlide6(){
                currentSlide = 6;
                $(changeSlide);
            }
            function selectSlide7(){
                currentSlide = 7;
                $(changeSlide);
            }
            function selectSlide8(){
                currentSlide = 8;
                $(changeSlide);
            }
            function selectSlide9(){
                currentSlide = 9;
                $(changeSlide);
            }
        </script>
        <style type ="text/css">
            #slider_wrapper_container {
                position: relative;
                display: block;
                width: 100vw;
                left: -24.15vw;
                height: auto;
            }

            .slider_wrapper {
                position: relative;
                display: block;
                margin: auto;
                margin-top: 50px;
                margin-bottom: 50px;
                width: 65vw;
                height: calc(36.562vw + 66px);
                border: 1px solid rgb(51, 51, 51);
                overflow: hidden;
            }

            .slide_window {
                display: flex;
                flex-direction: row;
              scroll-behavior: smooth;
                overflow-x: hidden;
            }

            .slide_frame {
                position: relative;
                display: block;
                width: 100%;
              height: 100px;
                flex-shrink: 0;
              border: 2px solid blue;
            }

            .slide_frame p{
              position: relative;
              display: block;
              width: 1em;
              margin: auto;
              font-size: 2em;
            }

            #frame_select {
                position: relative;
                display: block;
                margin: auto;
                padding-top: 20px;
                text-align: center;
                width: 85%;
            }

            .slider_controls {
                position: absolute;
                display: block;
                height: 66px;
                width: 100%;
                bottom: 0px;
            }

            .selector {
                padding: 5px;
                text-align: center;
                text-decoration: none;
                margin: 5px;
                border: 1px solid rgb(91, 91, 91);
                cursor: pointer;
            }

            .active_slide_control {
                background-color: black;
              color: white;
                cursor: default;
            }

            #left_arrow, #right_arrow {
                position: absolute;
                display: inline;
                height: 100%;
                padding: 10px;
                padding-top: 20px;
                cursor: pointer;
            }

            #left_arrow {
                float: left;
            }

            #right_arrow {
                float: right;
                right: 0px;
                top: 0px;
            }

            #right_arrow {
                float: right;
                border-bottom-right-radius: 40px 20px;
            }

            #left_control, #right_control {
                height: 50%;
            }
        </style>
    </head>
    <body>
        <div class="slider_wrapper">
            <div id="slideWindow" class="slide_window">
                <div id="slide_1" class="slide_frame">
                    <p>1</p>
                </div>
                <div id="slide_2" class="slide_frame">
                    <p>2</p>
                </div>
                <div id="slide_3" class="slide_frame">
                    <p>3</p>
                </div>
                <div id="slide_4" class="slide_frame">
                    <p>4</p>
                </div>
                <div id="slide_5" class="slide_frame">
                    <p>5</p>
                </div>
            </div>
            <div class="slider_controls">
                <div id="left_arrow" onclick="slidePrev()">
                    <a><--</a>
                </div>
                <div id="frame_select">
                </div>
                <div id="right_arrow" onclick="slideNext()">
                    <a>--></a>
                </div>
            </div>
        </div>
        <a id="readout">Current slide is #<span class="current_slide">?</span> out of <span class="total_slides">?</span> slides. The position.left value of currentSlide is <span class="position_left">?</span></a>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

各种各样的弄清楚了为什么我要从position()。左获得结果,而不是仔细研究CSS,我重新考虑了我使用变量的方式,这条路线。就像魅力一样。

singleSlideWidth = $(".slide_frame").width();
slideMeasure = currentSlide - 1;
currentSlideDistance = singleSlideWidth * slideMeasure;
$("#slideWindow").scrollLeft(currentSlideDistance);