简体   繁体   English

如何在整页半透明叠加层div上控制图像的CSS

[英]How to control css of images on top of a full page semi-transparent overlay div

I've got a full page overlay covering my page on load. 我有一个完整的页面覆盖了我的页面。 The only script I have attached to it is to close it if anything is clicked. 我附加到它的唯一脚本是,如果单击任何内容,则将其关闭。 The purpose of this overlay is instructional for the page, so I have some transparent .png images that I want to anchor to different areas of the page. 此叠加层的目的是为页面提供指导,因此我想将一些透明的.png图像锚定到页面的不同区域。

I have on listed as an example below, which I want to have full opacity and some spacing away from the top left corner of the page... however when I load it, its set to .4 transparency and its jammed into the upper left corner. 我在下面列出了一个示例,我希望它具有完全的不透明性,并且与页面的左上角有一定距离...但是,当我加载它时,其设置为.4透明性并且卡在了左上角角。 I'm no css expert, and I have been spinning my wheels with this. 我不是CSS专家,并且我一直在用这种方法纺车轮。 Can anyone help? 有人可以帮忙吗?

<script language="javascript" type="text/javascript">

    $(function () {

        var docHeight = $(document).height();

        $("body").append("<div id='overlay'><div id='home_text'><img src='/images/overlay_test_home.png'></div></div>");

        $("#overlay")
      .height(docHeight)
      .css({
          'opacity': 0.4,
          'position': 'absolute',
          'top': 0,
          'left': 0,
          'background-color': 'black',
          'width': '100%',
          'z-index': 5000
      });

        $("#home_text")
      .css({
          'opacity': 1.0,
           'top' : 20,
           'left': 200
      });

        $('#overlay').click(function () {
            $("#overlay").hide();
        });

    });

</script>

I would remove all html and css from the javascript it will make it more maintainable. 我会从javascript中删除所有html和css,这将使其更易于维护。

Here is your css: 这是您的CSS:

#overlay {
    background: rgba(0, 0, 0, .4);
    bottom: 0;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    z-index: 10;
}

#home_text {
    background: red;
    height: 300px;
    margin: 20px auto 0;
    width: 300px;
}

On the overlay I changed the opacity to rgba since opacity affects all content inside. 在覆盖层上,我将不透明度更改为rgba,因为不透明度会影响其中的所有内容。 Rgba is also not supported in all browsers so you can have a fallback transparent png, Modernizr is great for support checks. 并非所有浏览器均支持Rgba,因此您可以使用后备透明png,Modernizr非常适合进行支持检查。 Also removed the height setting on overlay and set the left right bottom and left to 0. You can change your home_text positioning to your liking I was unsure how it should look so I made it a red box. 还删除了叠加层上的高度设置,并将左下角和左下角设置为0。您可以根据自己的喜好更改home_text的位置,但不确定它的外观,因此将其设为红色框。

Here is your html: 这是您的html:

<div id="overlay">
    <div id="home_text">
        <!-- your image -->
    </div>
</div>

Here is you cleaned up js: 这是您清理的js:

$(function () {
    var $overlay = $('#overlay');
    $overlay.on('click', function (e) {
        $overlay
            .hide()
            .off();
    });
});

The off method removes that event. off方法删除该事件。

An example of this can be seen here: http://jsfiddle.net/SHBXd/1/ 可以在此处查看此示例: http : //jsfiddle.net/SHBXd/1/

Have you thought about using JQueryUI modal popup? 您是否考虑过使用JQueryUI模态弹出窗口? You can hide the title and customise it to how you want. 您可以隐藏标题并根据需要对其进行自定义。 http://jqueryui.com/dialog/ http://jqueryui.com/dialog/

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

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