zoukankan      html  css  js  c++  java
  • 简单的自定义弹出提示框

    效果如下:

    QQ20140724121326

    html代码:

    <div class="container">
                <div class="wrapper" style="background-color:white; position:relative;">
                    <div class="box" style="background-color:red; position:absolute; left:100px; top:300px; 100px;height:100px;">click on me!</div>
                </div>
            </div>

    CSS代码:

    #contentPopup
        {
            position: absolute;
            display: none;
            z-index:10;
        }
        #contentPopup
        {
            width:336px;
            
        }
        #contentPopup img
        {float:left;
         margin-left:110px;     
        }
        #contentPopup .popupcontent
        {
            background-color:#dddfe6;
            color:#192e59;
            font-weight:bold;
            font-size:16px;    
            width:300px;    
            float:left;  
            padding:16px;
            border-top-right-radius:10px;
    
        }
         #contentPopup .close
        {        
            left:190px;
            top:2px;
            position:absolute;
            cursor:pointer;    
        }
        .itemOver
        {
            color:#ffcb05;
        }

    Js/Jquery代码如下:

    function stopEvent() {
                    if (window.event)
                        event.cancelBubble = true;
                    else
                        e.stopPropagation();
                }
                (function () {
                    jQuery(document).ready(function ($) {
                        $("body").append('<div id="contentPopup" class="popup"><img class="close" src="img/x.png"/><div class="popupcontent"></div><img src="img/pop-up-arrow-down.png" /></div>');
    
                        $(".box").on("click", function (e) {
                            hidePopup();
                            applyBackground();
                            showPopup(e);
                        });
                        $(".close").on("click", function (e) {
                            hidePopup();
                        });
                    });
                    function applyBackground() {
                        if ($("#bg").length == 0) {
                            $("body").append('<div id="bg" class="popup" style="display:none;z-index:9;position:absolute;filter:alpha(opacity = 30);opacity:0.3;" onclick="hidePopup();"></div>');
                            $("#bg").height($(document).height()).width($(document).width()).css({ "left": "0px", "top": "0px" });
                        }
                        $("#bg").show();
                    }
                    function showPopup(e) {
                        var src = e.target ? e.target : e.srcElement;
                        if (!$(src).hasClass("box")) return;
                        $(src).addClass("itemOver");
                        var pos = $(src).offset();
                        $("#contentPopup").children("div").html("Please add your popup content here!");
                        $("#contentPopup").css({ "left": (pos.left - 70) + "px", "top": (pos.top - $("#contentPopup").height()) + "px" });
                        $("#contentPopup").show();
                    }
                    function hidePopup() {
                        $(".popup").hide();
    
                    }
                }());

    实现原理比较简单,预先把popup的内容绝对定位并设z-index为顶层,加载入body先隐藏,点击后就获取点击的节点元素位置按相应的位置show出来,要注意设置事件冒泡(stopEvent()),因为很可能你对box绑定了多层点击函数。

  • 相关阅读:
    【jsp】怎么在jsp文件中引入静态文件(.js .css)
    【MyBatis】MyBatis之分页
    【MyBatis】MyBatis之如何存储NULL
    【MyBatis】MyBatis之如何配置
    【MyBatis】MyBatis之别名typeAliases标签的使用
    【Spring】SpringMVC之详解AOP
    【Spring】SpringMVC之REST编程风格
    【Spring】SpringMVC之上传文件
    【Spring】SpringMVC之拦截器
    【Spring】SpringMVC之异常处理
  • 原文地址:https://www.cnblogs.com/fastmover/p/3865291.html
Copyright © 2011-2022 走看看