Javascript 在jquery中滑动元素时出现问题?

Javascript 在jquery中滑动元素时出现问题?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在尝试开发以下旋转木马 它应该以这种方式工作 单击向下按钮,旋转木马滚动并在中心调整始终为div的大小 我不能在反面应用相同的逻辑,所以当我点击按钮时,我需要收缩中央div,并向上滑动 我很高兴告诉你我做错了什么,如果你能在JSFIDLE上修复它。 另外,我想知道是否有更好的方法来达到同样的效果,或者是否有一个可以重用的组件 滚动文本框示例 #btn向上,#btn向下{ 位置:绝对位置; 顶部:600px; } #上升{ 左:0px; } #btn下降{ 左:50px; } #bt

我正在尝试开发以下旋转木马

它应该以这种方式工作

  • 单击向下按钮,旋转木马滚动并在中心调整始终为div的大小
我不能在反面应用相同的逻辑,所以当我点击按钮时,我需要收缩中央div,并向上滑动

我很高兴告诉你我做错了什么,如果你能在JSFIDLE上修复它。 另外,我想知道是否有更好的方法来达到同样的效果,或者是否有一个可以重用的组件



滚动文本框示例
#btn向上,#btn向下{
位置:绝对位置;
顶部:600px;
}
#上升{
左:0px;
}
#btn下降{
左:50px;
}
#btn向上,#btn向下{
宽度:50px;
高度:50px;
背景颜色:黄色;
外形:1px纯黑;
}
#内容滚动容器{
位置:绝对位置;
排名:0;
左:0px;
宽度:500px;
高度:250px;/*遮罩高度实际遮罩为200*/
溢出:隐藏;
}
#内容滚动内部{
位置:绝对位置;
}
.cnt{
高度:100px;
宽度:500px;
背景色:红色;
}
.cnt:n个孩子(偶数){
高度:100px;
背景颜色:粉红色;
}
变量滚动条={
配置:{
curPos:0,//位置
canExpand:false,
el:'内容滚动容器',//可见区域(容器)
Eliner:'内容滚动内部',//内部内容
cntPosY:null,//内容左上角(0位置)
cntHeight:null,//内容长度
maskHeight:null,//可见区域(其余区域被屏蔽)
canMoveUp:null,//true jquery可以向上滑动内容
canMoveDown:null,//true jquery可以向下滑动内容
增量:100,//用户执行操作时x像素的滑动
递增:0,//我们滑动了多少内容
animationSpeed:500,//jquery动画速度,使用0表示无动画
isAnimationOn:false,//当jquery执行动画时为true
},
数据:“012345空”,
getCntPosition:函数(){
//获取内容的y位置
var elm=document.getElementById(this.config.eliner);
this.config.cntPosY=elm.offsetTop;
},
getCntSize:函数(){
//获取内容的高度
var elm=document.getElementById(this.config.eliner);
this.config.cntwheight=elm.clientHeight;
},
getMaskSize:函数(){
//获取可见区域的高度
var elm=document.getElementById(this.config.el);
this.config.maskHeight=elm.clientHeight;
},
updateData:函数(){
//刷新状态
这个.getMaskSize();
这个.getCntPosition();
此参数为.getCntSize();
此参数为.canMoveUpCheck();
这个.canMoveDownCheck();
//console.clear();
log(this.config);
},
canMoveUpCheck:函数(){
//设置允许向上滑动的标志(以防我们有足够的内容显示)
var lastScreenY=(this.config.cntHeight-this.config.maskHeight);//最后一个屏幕可见
if((this.config.incrementOf*-1)=0)
this.config.canMoveDown=false;//如果内容位于开始位置(0位置),则无法向上移动更多内容
其他的
this.config.canMoveDown=true;
},
goUp:函数(){
//用户想要阅读以前的内容
//this.updateData();
if(this.config.canMoveDown==true&&this.config.isanimation==false){
this.moveCnt('down');//内容向下移动
}
},
货仓:函数(){//**************************
//用户希望阅读下一个内容
//this.updateData();
if(this.config.canMoveUp==true&&this.config.isanimation==false){
//检查新位置
var newPos=this.config.curPos+1;
如果(newPos>0){//特殊情况
if(this.config.canExpand==true)
this.config.increment=150;
this.config.canExpand=true;
这个.moveCnt('up');
}
}
},
moveCnt:功能(方向){
//做一个实际的
<!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Scroll text box example</title>
        <style>
            #btn-up, #btn-down {
                position: absolute;
                top: 600px;
            }

            #btn-up {
                left: 0px;
            }

            #btn-down {
                left: 50px;
            }

            #btn-up, #btn-down {
                width: 50px;
                height: 50px;
                background-color: yellow;
                outline: 1px solid black;
            }

            #content-scroll-container {
                position: absolute;
                top: 0;
                left: 0px;
                width: 500px;
                height: 250px; /* MASK HEIGHT real mask would be 200*/
                overflow: hidden;
            }

            #content-scroll-inner {
                position: absolute;
            }

            .cnt {
                height: 100px;
                width: 500px;
                background-color: red;
            }

                .cnt:nth-child(even) {
                    height: 100px;
                    background-color: pink;
                }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
            var scroller = {
                config: {
                    curPos: 0,              // position
                    canExpand: false,
                    el: 'content-scroll-container',     // visible area (container)
                    elInner: 'content-scroll-inner',    // inner content
                    cntPosY: null,          // content top-left corner (0 position)
                    cntHeight: null,        // content lenght
                    maskHeight: null,       // visible area (rest is masked out)
                    canMoveUp: null,        // true jquery can slide up content
                    canMoveDown: null,      // true jquery can slide down content
                    increment: 100,         // slide of x pixel when user perfom an action
                    incrementOf: 0,         // how much we have slided the contnt
                    animationSpeed: 500,    // jquery animation speed, use 0 for no animation
                    isAnimationOn: false,   // true when jquery is performing animation
                },
                data: '<div id="cnt-0" class="cnt">0</div><div id="cnt-1" class="cnt">1</div><div id="cnt-2" class="cnt">2</div><div id="cnt-3" class="cnt">3</div><div id="cnt-4" class="cnt">4</div><div id="cnt-5" class="cnt">5 empty</div>',
                getCntPosition: function () {
                    // get y position of content
                    var elm = document.getElementById(this.config.elInner);
                    this.config.cntPosY = elm.offsetTop;
                },
                getCntSize: function () {
                    // get height for content
                    var elm = document.getElementById(this.config.elInner);
                    this.config.cntHeight = elm.clientHeight;
                },
                getMaskSize: function () {
                    // get height visible area
                    var elm = document.getElementById(this.config.el);
                    this.config.maskHeight = elm.clientHeight;
                },
                updateData: function () {
                    // refresh state
                    this.getMaskSize();
                    this.getCntPosition();
                    this.getCntSize();
                    this.canMoveUpCheck();
                    this.canMoveDownCheck();
                    //console.clear();
                    console.log(this.config);
                },
                canMoveUpCheck: function () {
                    // set flags allowing sliding up (in case we have enought content to show)
                    var lastScreenY = (this.config.cntHeight - this.config.maskHeight); // last screen visible
                    if ((this.config.incrementOf * -1) < lastScreenY)
                        this.config.canMoveUp = true;
                    else
                        this.config.canMoveUp = false;
                },
                canMoveDownCheck: function () {
                    // set flags allowing sliding down (in case we have enought content to show)
                    if (this.config.cntPosY >= 0)
                        this.config.canMoveDown = false; // cannot move more up if content is on start position (0 position)
                    else
                        this.config.canMoveDown = true;
                },
                goUp: function () {
                    // user want to read previose content
                    //this.updateData();
                    if (this.config.canMoveDown == true && this.config.isAnimationOn == false) {
                        this.moveCnt('down'); // content go down
                    }
                },
                goDown: function () { //**************************
                    // user want to read next content
                    //this.updateData();
                    if (this.config.canMoveUp == true && this.config.isAnimationOn == false) {
                        // check newPos
                        var newPos = this.config.curPos + 1;
                        if (newPos > 0) { // special case
                            if (this.config.canExpand == true)
                                this.config.increment = 150;
                            this.config.canExpand = true;
                            this.moveCnt('up');
                        }



                    }
                },
                moveCnt: function (direction) {
                    // do the actual animation
                    var moveOf;
                    this.isAnimationOn = true;
                    if (direction == 'up') {
                        this.config.curPos++;
                        if (this.config.cntPosY == 0) { // special case for first item
                            moveOf = '-=' + (this.config.increment / 2);
                        }
                        else {
                            moveOf = '-=' + this.config.increment;
                        }
                        var target = '#' + this.config.elInner + ':not(:animated)';
                        $(target).animate({ 'top': moveOf }, this.animationSpeed, this.cbEndAnimation.bind(this, direction));
                    } else if (direction == 'down') {
                        this.config.curPos++;
                        var distanceToFp = (this.config.increment / 2); // height to reach first page (special page)
                        var distanceToFp = 50;
                        if (this.config.cntPosY == (distanceToFp * -1)) {
                            moveOf = '+=' + distanceToFp;
                            // i need to contract only the firs tone
                            $('cnt-1').css({ height: '100px' }, 500, this.cbEndAnimationExpand.bind(this));
                        } else {
                            moveOf = '+=' + this.config.increment;
                        }
                        var target = '#' + this.config.elInner + ':not(:animated)';
                        $(target).animate({ 'top': moveOf }, this.animationSpeed, this.cbEndAnimation.bind(this));

                    }
                    //var target = '#' + this.config.elInner + ':not(:animated)';
                    //$(target).animate({ 'top': moveOf }, this.animationSpeed, this.cbEndAnimation.bind(this, direction));
                },
                cbEndAnimation: function (direction) {
                    // runs when animation end
                    this.config.isAnimationOn = false;
                    if (direction == 'up') {
                        this.config.incrementOf -= this.config.increment;
                        if (this.config.canExpand == true) {    // expand
                            this.logicExpand();
                        } else {
                            // do nothing
                        }
                    }
                    else if (direction == 'down') {
                        this.config.incrementOf += this.config.increment;

                    }
                    this.updateData(); // refresh state has element has just moved
                    this.logicShowHideArrows();
                },
                logicExpand: function () {
                    // take contenf and expand it
                    var elm = document.getElementById('cnt-' + this.config.curPos);
                    $(elm).animate({ height: '150px' }, 500, this.cbEndAnimationExpand.bind(this));
                },
                cbEndAnimationExpand: function () {
                    console.log('end anim expand');
                },
                logicContract: function () {
                    var elm = document.getElementById('cnt-' + this.config.curPos);
                    $(elm).animate({ height: '-=50px' }, 500, this.cbEndAnimationContract.bind(this));
                },
                logicShowHideArrows: function () {
                    // reset first
                    this.hideArrow('btn-up');
                    this.hideArrow('btn-down');
                    if (this.config.canMoveUp == true)
                        this.showArrow('btn-down');
                    if (this.config.canMoveDown == true)
                        this.showArrow('btn-up');
                },
                cbEndAnimationContract: function () {
                    this.config.isAnimationOn = false;
                    this.moveCnt('down'); // content go down
                },
                showArrow: function (elmName) {
                    var elm = document.getElementById(elmName);
                    elm.style.display = '';
                },
                hideArrow: function (elmName) {
                    var elm = document.getElementById(elmName);
                    elm.style.display = 'none';
                },
                setEventHandler: function () {
                    // envet handlers for arrows
                    var btnUp = document.getElementById('btn-up');
                    btnUp.addEventListener('click', this.goUp.bind(this), false);
                    var btnDown = document.getElementById('btn-down');
                    btnDown.addEventListener('click', this.goDown.bind(this), false);
                },
                renderData: function () {
                    // render data content to slide
                    var elm = document.getElementById(this.config.elInner);
                    elm.innerHTML = this.data;
                },
                start: function () {
                    this.renderData();
                    this.updateData();
                    this.setEventHandler();
                    this.logicShowHideArrows(); // at start set arrows visibility
                }
            };
        </script>
    </head>
    <body onload="scroller.start();">
        <div id="content-scroll-container">
            <div id="content-scroll-inner">
            </div>
        </div>
        <div id="btn-up">UP</div>
        <div id="btn-down">DOWN</div>
    </body>
    </html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Scroll text box example</title>
    <style>
        #btn-up, #btn-down {
            position: absolute;
            top: 400px;
        }

        #btn-up {
            left: 0px;
        }

        #btn-down {
            left: 50px;
        }

        #btn-up, #btn-down {
            width: 50px;
            height: 50px;
            background-color: yellow;
            outline: 1px solid black;
        }

        #content-scroll-container {
            position: absolute;
            top: 0;
            left: 0px;
            width: 500px;
            height: 250px; /* MASK HEIGHT real mask would be 200*/
            overflow: hidden;
        }

        #content-scroll-inner {
            position: absolute;
        }

        .cnt {
            height: 100px;
            width: 500px;
            background-color: red;
        }

            .cnt:nth-child(even) {
                height: 100px;
                background-color: pink;
            }
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        var scroller = {
            config: {
                curPos: 0,              // position
                el: 'content-scroll-container',     // visible area (container)
                elInner: 'content-scroll-inner',    // inner content
                cntPosY: null,          // content top-left corner (0 position)
                cntHeight: null,        // content lenght
                maskHeight: null,       // visible area (rest is masked out)
                animationSpeed: 500,    // jquery animation speed, use 0 for no animation
                isAnimationOn: false,   // true when jquery is performing animation
            },
            data: '<div id="cnt-0" class="cnt">0</div><div id="cnt-1" class="cnt">1</div><div id="cnt-2" class="cnt">2</div><div id="cnt-3" class="cnt">3</div><div id="cnt-4" class="cnt">4</div><div id="cnt-5" class="cnt">5 empty</div>',
            getCntPosition: function () {
                // get y position of content
                var elm = document.getElementById(this.config.elInner);
                this.config.cntPosY = elm.offsetTop;
            },
            getCntSize: function () {
                // get height for content
                var elm = document.getElementById(this.config.elInner);
                this.config.cntHeight = elm.clientHeight;
            },
            getMaskSize: function () {
                // get height visible area
                var elm = document.getElementById(this.config.el);
                this.config.maskHeight = elm.clientHeight;
            },
            updateData: function () {
                // refresh state
                this.getMaskSize();
                this.getCntPosition();
                this.getCntSize();
                //console.clear();
                console.log(this.config);
            },
            logicShowHideArrows: function () {
                if(this.config.curPos<1) {
                    $('#btn-up').hide();
                } else {
                    $('#btn-up').show();
                }
                if(this.config.curPos>=4) {
                    $('#btn-down').hide();
                } else {
                    $('#btn-down').show();
                }
            },
            goUp: function () {

                if(this.config.curPos<4 && scroller.config.isAnimationOn ==false) {
                    scroller.config.isAnimationOn = true;
                    scroller.config.curPos++;
                    if(scroller.config.curPos==1) {
                        $('#content-scroll-inner').animate({'top':'-=50px'},500,function(){
                            $('#cnt-'+scroller.config.curPos).animate({'height':'+=50px'},500);
                            scroller.logicShowHideArrows();
                            scroller.config.isAnimationOn = false;
                        });
                        this.config.incrementOf-=50;
                        $('#btn-up').show();
                    }
                    else {
                         $('#content-scroll-inner').animate({'top':'-=150px'},500,function(){
                            $('#cnt-'+scroller.config.curPos).animate({'height':'+=50px'},500);
                            scroller.logicShowHideArrows();
                            scroller.config.isAnimationOn = false;
                        });
                            this.config.incrementOf-=150;
                    }
                    this.updateData();
                } 

            },
            goDown: function () { //**************************
                // user want to read next content
                //this.updateData();
                if(this.config.curPos>0 && scroller.config.isAnimationOn ==false) {
                    scroller.config.isAnimationOn = true;
                    if(this.config.curPos==1) {
                        $('#cnt-'+scroller.config.curPos).animate({'height':'-=50px'},500,function(){
                           $('#content-scroll-inner').animate({'top':'+=50px'},500);
                           scroller.logicShowHideArrows();
                           scroller.config.isAnimationOn = false;
                        });
                        scroller.config.curPos--;
                        this.config.incrementOf+=150;
                        this.updateData();
                    }
                    else {
                        $('#cnt-'+scroller.config.curPos).animate({'height':'-=50px'},500,function(){
                           $('#content-scroll-inner').animate({'top':'+=150px'},500);
                           scroller.logicShowHideArrows();
                           scroller.config.isAnimationOn = false;
                        });
                        scroller.config.curPos--;
                        this.config.incrementOf+=150;
                        this.updateData();
                    }
                }
            },

            setEventHandler: function () {
                $('#btn-up').click(function() {
                    scroller.goDown();
                });
                $('#btn-down').click(function() {
                    scroller.goUp();
                });
            },
            renderData: function () {
                // render data content to slide
                var elm = document.getElementById(this.config.elInner);
                elm.innerHTML = this.data;
            },
            start: function () {
                this.renderData();
                this.updateData();
                this.setEventHandler();
                this.logicShowHideArrows();
            }
        };
    </script>
</head>
<body onload="scroller.start();">
    <div id="content-scroll-container">
        <div id="content-scroll-inner">
        </div>
    </div>
    <div id="btn-up">UP</div>
    <div id="btn-down">DOWN</div>
</body>
</html>