单击并向下滚动页面上的滑入式调用操作div错误

时间:2016-05-26 23:01:17

标签: javascript jquery html css

我有一个div,点击它会在右侧滑入,但我还试图在滚动时滑入它,如果它尚未打开或打开。因此,您可以单击按钮将其滑入或关闭,当您向下滚动页面时它会打开,然后在几秒钟后消失或者单击按钮。

我有点工作,但非常错。任何帮助将不胜感激!

http://jsfiddle.net/abennington/50tab7vh/2/

HTML:

<div class="floating-form" id="contact_form">
<div class="contact-opener">CTA Box Toggle</div>    
    <div class="contact_body">

        <h3>This is a CTA Box</h3>
        <p>On click it will slide out and on scroll it will scroll out as you get down the page and then go away.</p>

        <a href="#" class="btn btn-primary btn-block"><i class="fa fa-calendar-o"></i> CTA Button</a>


    </div>
</div>

jQuery的:

$(document).ready(function(){ 
    var buttonhieght = $('.contact-opener').width()-52;
    var popdiv = $('#contact_form').width();
    var _scroll = true, _timer = false, _floatbox = $("#contact_form"), _floatbox_opener = $(".contact-opener") ;
    _floatbox.css("right", "-"+ popdiv+"px"); //initial contact form position
    _floatbox_opener.css("left", "-"+ buttonhieght+"px"); //initial contact form position

    //Contact form Opener button
    _floatbox_opener.click(function(){
        if (_floatbox.hasClass('visiable')){
            _floatbox.animate({'right': '-'+ popdiv+'px'}, {duration: 300}).removeClass('visiable');
        }else{
           _floatbox.animate({"right":"0px"},  {duration: 300}).addClass('visiable');
        }
    }); 
});
$(document).on('scroll', function() {
    var popdiv = $('#contact_form').width();
    var _floatbox = $("#contact_form");
    var scrollTop = $(window).scrollTop();
    if (scrollTop > 700) {
        if (_floatbox.hasClass('visiable')){
            _floatbox.animate({'right': '-'+ popdiv+'px'}, {duration: 300}).removeClass('visiable');
        }else{
           _floatbox.animate({"right":"0px"},  {duration: 300}).addClass('visiable');
        }
    }
})

CSS:

/* floating box style */
.floating-form * { color: #fff; }
.floating-form {
    width:300px;
    max-width: 300px;
    top: 100px;
    font: 13px Arial, Helvetica, sans-serif;
    background: #F9F9F9;
    background: #754c24;
    color: #fff;    
    right: 10px;
    position: fixed;
    z-index: 1000;
    box-shadow: -2px -0px 8px rgba(43, 33, 33, 0.06);
    -moz-box-shadow: -2px -0px 8px rgba(43, 33, 33, 0.06);
    -webkit-box-shadow:  -2px -0px 8px rgba(43, 33, 33, 0.06);
    }
.contact-opener {
    position: absolute;
    text-transform:uppercase;   
    left: -100px;
    transform: rotate(-90deg);
    top: 150px;
    background-color: #754c24;
    padding: 10px 15px;
    color: #fff;
    text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.43);
    cursor: pointer;
    border-radius: 5px 5px 0px 0px;
    -webkit-border-radius: 5px 5px 0px 0px;
    -moz-border-radius: 5px 5px 0px 0px;
    box-shadow: -2px -0px 8px rgba(43, 33, 33, 0.06);
    -moz-box-shadow: -2px -0px 8px rgba(43, 33, 33, 0.06);
    -webkit-box-shadow:  -2px -0px 8px rgba(43, 33, 33, 0.06);

}
#contact_form .contact_body { padding: 30px 30px 10px 30px; }
#contact_form .contact_body h3 { margin: 0rem 0 1.0rem; }
#contact_form .contact_body ol { padding: 5px 0px 10px 20px; }
#contact_form .contact_body li { padding: 8px 0px; font-weight: 700; }
#contact_form .contact_body .btn-primary { background: rgba(35,26,19,0.9); margin-bottom: 20px; }

1 个答案:

答案 0 :(得分:0)

问题是滚动处理程序永远不会分离,因此#contact_form会一直显示/隐藏,直到所有滚动事件都被“清除”。我已经更新了你的小提琴,只有当页面滚动到某个点后才会尝试显示#contact_form,然后删除滚动处理程序。

我认为这就是你想要的:http://jsfiddle.net/50tab7vh/3/