javascript - 更新 CSS 过渡

标签 javascript jquery html css

我有三个 div,我想在刷新时将左边的那个滑动到页面左侧,当鼠标点击左边缘时我希望 div 向右滑动。

这是 fiddle :https://jsfiddle.net/8qxua9Lx/1/

问题是 CSS transition 属性第一次起作用,但是当我使用 jQuery 再次使用 transform 时它第二次不起作用。我该如何解决这个问题?

这是JS代码:

var toggleEdges = function(width) {
                var end = true;
                var slideOutLeftEdge = function() {
                    $('.leftAnchor').attr('class', 'leftAnchor').addClass("slideOutLeftEdge").delay(1000).queue(function(){
                        $('.leftAnchor').removeClass('slideOutLeftEdge');
                        $('.leftAnchor').css('left', '-500px');
                        $('.leftAnchor').hide();
                        $(this).clearQueue();
                        $( this ).dequeue();
                    });
                };
                var slideInLeftEdge = function() {
                    $('.leftAnchor').show();
                    $('.leftAnchor').attr('class', 'leftAnchor').addClass("slideInLeftEdge").delay(1000).queue(function(){
                        $('.leftAnchor').removeClass('slideInLeftEdge');
                        $('.leftAnchor').css('left', '0');
                        $(this).clearQueue();
                        $( this ).dequeue();
                    });
                };
                var slideOutRightEdge = function() {
                    $('.rightAnchor').attr('class', 'rightAnchor').addClass("slideOutRightEdge").delay(1000).queue(function(){
                        $('.rightAnchor').hide();
                    });
                };
                var slideInRightEdge = function() {
                    $('.rightAnchor').show();
                    $('.rightAnchor').attr('class', 'rightAnchor').addClass("slideInRightEdge").delay(1000);
                };

                $(document).on('mousemove', function(event) {
                    if (event.pageX > width && $('.leftAnchor').is(':visible')) {
                        slideOutLeftEdge();
                    }
                    if (event.pageX < 10 && !$('.leftAnchor').is(':visible')) {
                        slideInLeftEdge();
                    }
                    if (event.pageX < window.innerWidth - width && $('.rightAnchor').is(':visible')) {
                        //slideOutRightEdge();
                    }
                    if (event.pageX > window.innerWidth - 10 && !$('.rightAnchor').is(':visible')) {
                        //slideInRightEdge();
                    }
                });
            };
            toggleEdges(500);

最佳答案

您在这里遇到的问题是因为您的 div 可见并且您检查了 !$('.leftAnchor').is(':visible')。 .

我做了一些更改以重用 jQuery 对象,而不是每次需要时都创建它们($leftAnchor$rightAnchor)

var toggleEdges = function (width) {
    var end = true;
    var $leftAnchor = $('.leftAnchor');
    var $rightAnchor = $('.rightAnchor');

    var slideOutLeftEdge = function () {
        $leftAnchor.attr('class', 'leftAnchor').addClass("slideOutLeftEdge").delay(1000).queue(function () {
            console.log("slideOutLeftEdge");
            $leftAnchor.removeClass('slideOutLeftEdge');
            $leftAnchor.css('left', '-500px');
            //$leftAnchor.hide();
            $(this).clearQueue();
            $(this).dequeue();
        });
    };
    var slideInLeftEdge = function () {
        $leftAnchor.show();
        $leftAnchor.attr('class', 'leftAnchor').addClass("slideInLeftEdge").delay(1000).queue(function () {
            console.log("slideInLeftEdge");
            //$leftAnchor.removeClass('slideInLeftEdge');
            //$leftAnchor.css('left', '0');
            $(this).clearQueue();
            $(this).dequeue();
        });
    };
    var slideOutRightEdge = function () {
        $rightAnchor.attr('class', 'rightAnchor').addClass("slideOutRightEdge").delay(1000).queue(function () {
            $rightAnchor.hide();
        });
    };
    var slideInRightEdge = function () {
        $rightAnchor.show();
        $rightAnchor.attr('class', 'rightAnchor').addClass("slideInRightEdge").delay(1000);
    };

    $(document).on('mousemove', function (event) {
        if (event.pageX > width && $leftAnchor.is(':visible')) {
            slideOutLeftEdge();
        }
        if (event.pageX < 10 && $leftAnchor.css('left') === '-500px') {
            slideInLeftEdge();
        }
        if (event.pageX < window.innerWidth - width && $rightAnchor.is(':visible')) {
            //slideOutRightEdge();
        }
        if (event.pageX > window.innerWidth - 10 && !$rightAnchor.is(':visible')) {
            //slideInRightEdge();
        }
    });
};
toggleEdges(500);

我更新了你的 jsfiddle - https://jsfiddle.net/8qxua9Lx/6/

如果这是您的需要,那么您可以改进代码以使其使用正确的 div

关于javascript - 更新 CSS 过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35584476/

相关文章:

javascript - 完全坚持将 JQuery 加载到 WordPress 站点

html - Github 和 CSS 协助

javascript - 始终将文本光标设置在文本框上

javascript - 更改 Google 可视化时间轴中列描边的颜色

javascript - JQuery 使用 .next ('.selector' 选择下一个 .selector 元素)

javascript - Javascript 中的 Excel 到 JSON 模式

javascript - JCrop 为图像添加高度和宽度

PHP 递归目录列表 - 仅目录

html - ie7 中的悬停问题

javascript - jquery限制输入框内的文字