简体   繁体   English

jQuery模糊div不在焦点

[英]jQuery blur div not in focus

EDIT: You can see my code working here: unf.edu/~n00804716/site/work.html 编辑:你可以看到我的代码在这里工作:unf.edu/~n00804716/site/work.html

I am attempting to blur a section with jQuery when I open an iframe. 当我打开iframe时,我试图用jQuery模糊一个部分。 The iframe is initially hidden, but appears as a fixed element above everything when a button is clicked. iframe最初是隐藏的,但在单击按钮时显示为固定元素。 I have found one way to do this, but it requires an excessive amount of code. 我找到了一种方法,但它需要过多的代码。 So, I tried to cut down a little, but can't get it to work. 所以,我试图减少一点,但不能让它工作。 Here is what I have: 这是我有的:

$('#slides iframe').hide();

$("span").click(function() {
    $("#slides iframe").fadeIn(300);
});

$('#slides iframe').each(function(){
    if ( $(this).css('display') == 'block')
    {
        $("section").css({
            'filter': 'blur(15px) greyscale(80%)'
        });
    } else {
        $("section").css({
            'filter': 'blur(0px) greyscale(0%)'
        });
    }
});

This is how my HTML is setup: 这就是我的HTML设置方式:

<div id="slides">
   <div id="slide-2" class="slide">
      <iframe class="zoo-video"></iframe>
      <section>
          <span></span>
          /*other content*/
      </section>
   </div>
</div>

CSS: CSS:

#slides iframe {
    width: 90%;
    margin: 0 auto;
    height: 90%;
    z-index: 9999;
    position: absolute;
    right: 0;
    left: 0;
    top: 5%;
    bottom: 0;
}

#slides,
section {
    width: 100%;
}

Also, I'm not entirely sure if the vendor prefixes are necessary. 另外,我不完全确定供应商前缀是否必要。 Is there a much simpler way to do this? 有没有更简单的方法来做到这一点? The trick is, the iframe is a vimeo player that takes up 90% of the screen. 诀窍是,iframe是一个占据屏幕90%的vimeo播放器。 So, I also need the iframe to close/collapse when the page is scrolled vertically or if the user clicks outside of the iframe. 因此,当页面垂直滚动或用户点击iframe外部时,我还需要iframe关闭/折叠。 When it collapses, I need it the section to no longer have a blur or grayscale. 当它崩溃时,我需要它的部分不再有模糊或灰度。 Here is the code I'm using to collapse the iframe when the user clicks outside of it: 以下是我用来在用户点击iframe时折叠iframe的代码:

$(document).mouseup(function (e) {
    var container = $("#slides iframe");
    if (!container.is(e.target)
        && container.has(e.target).length === 0)
    {
        container.fadeOut(230);
    }
});

http://jsfiddle.net/721nma0g/ http://jsfiddle.net/721nma0g/

$(document).ready(function () {
    var video = $("#slides iframe");
    $(document).mouseup(function (e) {
        if (!video.is(e.target) && !video.has(e.target).length) {
            video.removeClass("visible-video");
        }
    });

    $("button").click(function() {
        video.addClass("visible-video");
    });
});

Instead of doing so much with jQuery, I often just use JS to set a class. 我没有用jQuery做这么多,而是经常只用JS来设置一个类。 You can do so much more with CSS than you'd think. 使用CSS可以做到比你想象的更多。 Of importance is the CSS. 重要的是CSS。 You had a small spelling error (US vs UK spelling) greyscale vs grayscale . 你有一个小的拼写错误(美国与英国的拼写) greyscalegrayscale But as you can see the + selector is very useful here! 但是你可以看到+选择器在这里非常有用! Select the element following .visible-video and you can target the section that you want! 选择.visible-video后面的元素,你可以定位你想要的部分!

iframe {
    display: none; /* Hide the video initially */
    width: 90%;
    margin: auto 5%;
    position: fixed;
    z-index: 20;
    top: 48px;
    box-shadow: 0 4px 36px rgba(0,0,0,0.72), 0 0 8px rgba(0,0,0,0.48);
}

.visible-video {
    display: block; /* show the video when the class has been set */
}
section {
    margin: 24px auto;
    width: 80%;
}
.visible-video + section {
    -webkit-filter: blur(15px) grayscale(80%);
    filter: blur(15px) grayscale(80%);
}

EDIT: I improved the CSS code of the iframe, so it'll scale better, and I included some JS that will detect if the user has clicked on the scrollbar or not. 编辑:我改进了iframe的CSS代码,所以它会更好地扩展,并且我包含了一些JS,它将检测用户是否点击了滚动条。 Without this addition, the iframe will also close when clicking the scrollbar. 如果没有这个添加,iframe也会在单击滚动条时关闭。

Full screen result here: https://jsfiddle.net/721nma0g/5/embedded/result/ 全屏结果: https//jsfiddle.net/721nma0g/5/embedded/result/

Edited jQuery: 编辑jQuery:

if (!video.is(e.target) && !video.has(e.target).length && (e.target != $('html').get(0))) {
    video.removeClass("visible-video");
}

Edited CSS: 编辑CSS:

iframe {
    display: none;
    max-width: 90%;
    position: fixed;
    z-index: 20;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 4px 36px rgba(0, 0, 0, 0.72), 0 0 8px rgba(0, 0, 0, 0.48);
}

Works great on mobile too. 在移动设备上也很棒。

移动结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM