简体   繁体   English

jQuery中的灯箱弹出表单

[英]Light box pop up form in jquery

Here is my code: 这是我的代码:

<a href="https://dev88.wufoo.com/forms/ze7xusq0j2tum9/" id="test">Please fill out my form.</a>
<script>
    var test = document.getElementById('test');
    var win = null;
        test.addEventListener('click', function(e) {
        e.preventDefault();
        e.stopPropagation();

        win = window.open(test.href,  null, 'height=823, width=680, toolbar=0, location=0, status=1, scrollbars=1, resizable=1');
        return false;
    });
        window.addEventListener('click', function(e) {
        if(win != null) {
            win.close();
            win = null;
        }
    });
</script>

This code works fine, but i need like to display as light box, for example please refer this site, http://fancybox.net/ ,, I am new to javascript, can anyone one help me to do this, 这段代码可以正常工作,但是我需要显示为灯箱,例如,请访问此网站http://fancybox.net/ ,我是javascript的新手,任何人都可以帮助我做到这一点,

Any help would be appreciated, Thank you. 任何帮助将不胜感激,谢谢。

To start working with javascript, you would need a javascript library API. 要开始使用javascript,您需要一个javascript库API。 You must have heard about JQuery , this makes your work easier than regular Javascript codes. 您一定已经听说过JQuery ,这比常规的Javascript代码使您的工作更轻松。 JQuery have lots of plugins especially for lightbox gallery that you are looking for. jQuery有很多插件,尤其是您正在寻找的灯箱画廊。 One useful lightbox plugin is Colorbox . 一个有用的灯箱插件是Colorbox

Start by importing the libraries to your header just like below. 首先将库导入到标题中,如下所示。 You also might need some css files for the colorbox themes. 您可能还需要一些css文件来实现colorbox主题。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
<script src="../jquery.colorbox.js"></script>

Then start using colorbox just like below 然后像下面一样开始使用colorbox

<a href="https://dev88.wufoo.com/forms/ze7xusq0j2tum9/" id="test">Please fill out my form.</a>

$(document).ready(function(){
    $("#test").click(function(){ //on clicking the link above
           $(this).colorbox({ //this would capture the url from the href
                        width:'500px', //width of the colorbox
                        height:'auto', //height of the colorbox
                        initialWidth:'500px', //initial width upon loading
                        initialHeight:'auto', //initial height upon loading
                        speed:0, //animation speed
                        opacity:0.2, //background opacity
                        overlayClose: true, //close upon clicking anywhere
                        title:'Your Form Title', //your form title name
                        onComplete: function(){
                            //do something when the colorbox loaded completely 
                        }
                });
    })
});

Take a look on this following example. 看下面的例子。 This is custom light box without any plugin: 这是没有任何插件的自定义灯箱:

Updated Fiddle 更新小提琴

jQuery: jQuery的:

var lightBox = $('#lightbox'),
    lightBoxContent = $('#lb-content');

var positionLightbox = function() {
    var veiwWidth = $(window).width(),
        lbContentMargin = (veiwWidth / 2) - 148,
        lbContent = $('#lb-content');

    lbContent.css({
        'left' : lbContentMargin,
        'top' : $(window).scrollTop() + 50 + 'px'
    });
};

$('#test').click(function(e) {
    e.preventDefault();
    lightBox.fadeIn(function() {
        lightBoxContent.show();                               
    });
    positionLightbox();
});

$('#lb-close').click(function() {
    lightBox.hide();
    lightBoxContent.hide();
});
/*hide click outside*/
$(document).mouseup(function (e)
{
    if (!lightBoxContent.is(e.target) // if the target of the click isn't the container...
        && lightBoxContent.has(e.target).length === 0) // ... nor a descendant of the container
    {
        lightBox.hide();
        lightBoxContent.hide();
    }
});

CSS: CSS:

body {color: #fff; font-family: arial; font-family: arial;}
#lightbox {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.8;
    text-align: center;
    display: none;
}  
#lb-content {
    color: #222;
    height: 150px;
    width: 260px;
    border: 16px solid #222;
    background-color: #fff;
    position: relative;
    text-align: center;
    padding-top: 10px;
    border-radius: 4px;
    display: none;
}
#lb-close {
    display: block;
    height: 22px;
    width: 25px;
    background-color: red;
    color: #fff;
    position: absolute;
    top: -25px;
    right: -25px; 
    cursor: pointer;
    text-align: center;   
    border-radius: 10px;    
}

You can go for jQuery Plugin also: 您还可以使用jQuery插件:

  1. http://lokeshdhakar.com/projects/lightbox2/ http://lokeshdhakar.com/projects/lightbox2/
  2. http://dimsemenov.com/plugins/magnific-popup/ http://dimsemenov.com/plugins/magnific-popup/
  3. http://www.jacklmoore.com/colorbox/ http://www.jacklmoore.com/colorbox/

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

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