javascript - 在另一个函数中向左/向右移动 div

标签 javascript jquery html css

当图像像这样左右移动时,尝试左右移动“sliding_head”div:

Google Docs sliding content

这是我的 CSS、HTML 和 JS(包括我的尝试)。我的尝试不起作用,因为我将值设置在左/右函数之外,而当我将它设置在其中时,它就不起作用了。向左滑动时标题需要向左滑动 509px,向右滑动时返回到起始位置。我如何调整下面的 JS 以在插件函数中执行此操作?

        #second {
            position:absolute;
            left:1024px;
            width:2048px;
            height:768px;
            background-color: #f3f0ef;

        }

        .sliding_content {

        }

        .sliding_head {
            position: absolute;
            top: 119px;
            left: 140px;
        }

        .sliding_2 {
            position: absolute;
            left: 1024px;
        }

        .imgs img{
            float: left;
        }

HTML

<div id="second">
                    <img class="notes" src="img/2-top.jpg" />
                     <img src="img/2-top-2.jpg" />
                     <img src="img/2-middle.jpg" />
                        <div class="sliding_content">
                            <img class="sliding_head" src="img/2-sliding_head.png" /> <!-- this is the header image that needs to slide 509px left when swiping left and back to its start position when swiping right -->

                            <div class="galleryTouch">
                              <div class="imgs">
                                <img src="img/2-sliding_1.png">
                                <img src="img/2-sliding_2.png">
                              </div>
                            </div>
                        </div>
                </div>

为此尝试了 JS(我的评论内嵌在大写字母中)

$(function() {
        var IMG_WIDTH = 1024,
        currentImg=0,
        maxImages=2;
        speed=500,
        imgs = $(".imgs");
        head = $(".sliding_head"); //ADDED VAR FOR ADDED SLIDING DIV



        //Init touch swipe
        imgs.swipe( {
            triggerOnTouchEnd : true,
            swipeStatus : swipeStatus,
            allowPageScroll:"vertical"
        });


        /**
        * Catch each phase of the swipe.
        * move : we drag the div.
        * cancel : we animate back to where we were
        * end : we animate to the next image
        */
        function swipeStatus(event, phase, direction, distance, fingers)
        {
            //If we are moving before swipe, and we are going L or R, then manually drag the images
            if( phase=="move" && (direction=="left" || direction=="right") )
            {
                var duration=0;

                if (direction == "left") 
                    scrollImages((IMG_WIDTH * currentImg) + distance, duration);
                    //I'VE TRIED HERE
                else if (direction == "right")
                    scrollImages((IMG_WIDTH * currentImg) - distance, duration);
                    //AND HERE BUT IT STOPS WORKING
            }

            //Else, cancel means snap back to the begining
            else if ( phase == "cancel")
            {
                scrollImages(IMG_WIDTH * currentImg, speed);
            }

            //Else end means the swipe was completed, so move to the next image
            else if ( phase =="end" )
            {
                if (direction == "right")
                    previousImage();
                else if (direction == "left")
                    nextImage()
            }
        }

        function previousImage()
        {
            currentImg = Math.max(currentImg-1, 0);
            scrollImages( IMG_WIDTH * currentImg, speed);
        }

        function nextImage()
        {
            currentImg = Math.min(currentImg+1, maxImages-1);
            scrollImages( IMG_WIDTH * currentImg, speed);
        }

        /**
         * Manually update the position of the imgs on drag
         */
        function scrollImages(distance, duration)
        {
            imgs.css("-webkit-transition-duration", (duration/1000).toFixed(1) + "s");
            //inverse the number we set in the css
            var value = (distance<0 ? "" : "-") + Math.abs(distance).toString();
            imgs.css("-webkit-transform", "translate3d("+value +"px,0px,0px)");

            //ADDED THIS IN MY LATEST ATTEMPT, IT JUST SLIDES TO NEW POSITION BUT NOT BACK
            head.css("-webkit-transition-duration", (duration/500).toFixed(1) + "s"); //DO THIS ONE SLOWER
            //MAKE 'VALUE' AN INT (WILL BE -1024) AND SET IN NEW VAR PLUS 509 LEFT TO POSITION CORRECTLY
            var headval = parseInt(value)+509;
            head.css("-webkit-transform", "translate3d("+headval +"px,0px,0px)");

        }
    });

最佳答案

通过在 if (direction == "left") 和 if (direction == "right") 中设置我的移动来解决。

关于javascript - 在另一个函数中向左/向右移动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23899690/

相关文章:

JavaScript 绑定(bind)方法不适用于 getter 属性

javascript - Express js,将尾随 "/"添加到所有网址

javascript - Jquery在jQuery错误中从缩略图放大图像

javascript - 如何解决 'Redirect has been blocked by CORS policy: No ' Access-Control-Allow-Origin' header'?

Javascript : Insane boolean test with '!' operator

javascript - 如何停止javascript中的函数?

javascript - JQuery .click 函数不适用于包含的 <a> 标签

javascript - Internet Explorer 中的 HTML5 拖放问题(无法访问 dataTransfer 属性)

java - 如何从 Jfree 图表的图像中获取图像映射

javascript - 如何在 vanilla javascript 中用更好的替代方法替换 onclick?