"Magnific Popup" and leaving when hovering a div that opens a popup - javascript
After closing the popup, the div should be hidden (and when the pop-up is open it really hides), but when the pop-up is closed, it becomes visible.
// Magnific Popup v1.1.0 by Dmitry Semenov
(function(a){typeof define=="function"&&define.amd?define(["jquery"],a):typeof exports=="object"?a(require("jquery")):a(window.jQuery||window.Zepto)})(function(a){var b="Close",c="BeforeClose",d="AfterClose",e="BeforeAppend",f="MarkupParse",g="Open",h="Change",i="mfp",j="."+i,k="mfp-ready",l="mfp-removing",m="mfp-prevent-close",n,o=function(){},p=!!window.jQuery,q,r=a(window),s,t,u,v,w=function(a,b){n.ev.on(i+a+j,b)},x=function(b,c,d,e){var f=document.createElement("div");return f.className="mfp-"+b,d&&(f.innerHTML=d),e?c&&c.appendChild(f):(f=a(f),c&&f.appendTo(c)),f},y=function(b,c){n.ev.triggerHandler(i+b,c),n.st.callbacks&&(b=b.charAt(0).toLowerCase()+b.slice(1),n.st.callbacks[b]&&n.st.callbacks[b].apply(n,a.isArray(c)?c:[c]))},z=function(b){if(b!==v||!n.currTemplate.closeBtn)n.currTemplate.closeBtn=a(n.st.closeMarkup.replace("%title%",n.st.tClose)),v=b;return n.currTemplate.closeBtn},A=function(){a.magnificPopup.instance||(n=new o,n.init(),a.magnificPopup.instance=n)},B=function(){var a=document.createElement("p").style,b=["ms","O","Moz","Webkit"];if(a.transition!==undefined)return!0;while(b.length)if(b.pop()+"Transition"in a)return!0;return!1};o.prototype={constructor:o,init:function(){var b=navigator.appVersion;n.isLowIE=n.isIE8=document.all&&!document.addEventListener,n.isAndroid=/android/gi.test(b),n.isIOS=/iphone|ipad|ipod/gi.test(b),n.supportsTransition=B(),n.probablyMobile=n.isAndroid||n.isIOS||/(Opera Mini)|Kindle|webOS|BlackBerry|(Opera Mobi)|(Windows Phone)|IEMobile/i.test(navigator.userAgent),s=a(document),n.popupsCache={}},open:function(b){var c;if(b.isObj===!1){n.items=b.items.toArray(),n.index=0;var d=b.items,e;for(c=0;c<d.length;c++){e=d[c],e.parsed&&(e=e.el[0]);if(e===b.el[0]){n.index=c;break}}}else n.items=a.isArray(b.items)?b.items:[b.items],n.index=b.index||0;if(n.isOpen){n.updateItemHTML();return}n.types=[],u="",b.mainEl&&b.mainEl.length?n.ev=b.mainEl.eq(0):n.ev=s,b.key?(n.popupsCache[b.key]||(n.popupsCache[b.key]={}),n.currTemplate=n.popupsCache[b.key]):n.currTemplate={},n.st=a.extend(!0,{},a.magnificPopup.defaults,b),n.fixedContentPos=n.st.fixedContentPos==="auto"?!n.probablyMobile:n.st.fixedContentPos,n.st.modal&&(n.st.closeOnContentClick=!1,n.st.closeOnBgClick=!1,n.st.showCloseBtn=!1,n.st.enableEscapeKey=!1),n.bgOverlay||(n.bgOverlay=x("bg").on("click"+j,function(){n.close()}),n.wrap=x("wrap").attr("tabindex",-1).on("click"+j,function(a){n._checkIfClose(a.target)&&n.close()}),n.container=x("container",n.wrap)),n.contentContainer=x("content"),n.st.preloader&&(n.preloader=x("preloader",n.container,n.st.tLoading));var h=a.magnificPopup.modules;for(c=0;c<h.length;c++){var i=h[c];i=i.charAt(0).toUpperCase()+i.slice(1),n["init"+i].call(n)}y("BeforeOpen"),n.st.showCloseBtn&&(n.st.closeBtnInside?(w(f,function(a,b,c,d){c.close_replaceWith=z(d.type)}),u+=" mfp-close-btn-in"):n.wrap.append(z())),n.st.alignTop&&(u+=" mfp-align-top"),n.fixedContentPos?n.wrap.css({overflow:n.st.overflowY,overflowX:"hidden",overflowY:n.st.overflowY}):n.wrap.css({top:r.scrollTop(),position:"absolute"}),(n.st.fixedBgPos===!1||n.st.fixedBgPos==="auto"&&!n.fixedContentPos)&&n.bgOverlay.css({height:s.height(),position:"absolute"}),n.st.enableEscapeKey&&s.on("keyup"+j,function(a){a.keyCode===27&&n.close()}),r.on("resize"+j,function(){n.updateSize()}),n.st.closeOnContentClick||(u+=" mfp-auto-cursor"),u&&n.wrap.addClass(u);var l=n.wH=r.height(),m={};if(n.fixedContentPos&&n._hasScrollBar(l)){var o=n._getScrollbarSize();o&&(m.marginRight=o)}n.fixedContentPos&&(n.isIE7?a("body, html").css("overflow","hidden"):m.overflow="hidden");var p=n.st.mainClass;return n.isIE7&&(p+=" mfp-ie7"),p&&n._addClassToMFP(p),n.updateItemHTML(),y("BuildControls"),a("html").css(m),n.bgOverlay.add(n.wrap).prependTo(n.st.prependTo||a(document.body)),n._lastFocusedEl=document.activeElement,setTimeout(function(){n.content?(n._addClassToMFP(k),n._setFocus()):n.bgOverlay.addClass(k),s.on("focusin"+j,n._onFocusIn)},16),n.isOpen=!0,n.updateSize(l),y(g),b},close:function(){if(!n.isOpen)return;y(c),n.isOpen=!1,n.st.removalDelay&&!n.isLowIE&&n.supportsTransition?(n._addClassToMFP(l),setTimeout(function(){n._close()},n.st.removalDelay)):n._close()},_close:function(){y(b);var c=l+" "+k+" ";n.bgOverlay.detach(),n.wrap.detach(),n.container.empty(),n.st.mainClass&&(c+=n.st.mainClass+" "),n._removeClassFromMFP(c);if(n.fixedContentPos){var e={marginRight:""};n.isIE7?a("body, html").css("overflow",""):e.overflow="",a("html").css(e)}s.off("keyup"+j+" focusin"+j),n.ev.off(j),n.wrap.attr("class","mfp-wrap").removeAttr("style"),n.bgOverlay.attr("class","mfp-bg"),n.container.attr("class","mfp-container"),n.st.showCloseBtn&&(!n.st.closeBtnInside||n.currTemplate[n.currItem.type]===!0)&&n.currTemplate.closeBtn&&n.currTemplate.closeBtn.detach(),n.st.autoFocusLast&&n._lastFocusedEl&&a(n._lastFocusedEl).focus(),n.currItem=null,n.content=null,n.currTemplate=null,n.prevHeight=0,y(d)},updateSize:function(a){if(n.isIOS){var b=document.documentElement.clientWidth/window.innerWidth,c=window.innerHeight*b;n.wrap.css("height",c),n.wH=c}else n.wH=a||r.height();n.fixedContentPos||n.wrap.css("height",n.wH),y("Resize")},updateItemHTML:function(){var b=n.items[n.index];n.contentContainer.detach(),n.content&&n.content.detach(),b.parsed||(b=n.parseEl(n.index));var c=b.type;y("BeforeChange",[n.currItem?n.currItem.type:"",c]),n.currItem=b;if(!n.currTemplate[c]){var d=n.st[c]?n.st[c].markup:!1;y("FirstMarkupParse",d),d?n.currTemplate[c]=a(d):n.currTemplate[c]=!0}t&&t!==b.type&&n.container.removeClass("mfp-"+t+"-holder");var e=n["get"+c.charAt(0).toUpperCase()+c.slice(1)](b,n.currTemplate[c]);n.appendContent(e,c),b.preloaded=!0,y(h,b),t=b.type,n.container.prepend(n.contentContainer),y("AfterChange")},appendContent:function(a,b){n.content=a,a?n.st.showCloseBtn&&n.st.closeBtnInside&&n.currTemplate[b]===!0?n.content.find(".mfp-close").length||n.content.append(z()):n.content=a:n.content="",y(e),n.container.addClass("mfp-"+b+"-holder"),n.contentContainer.append(n.content)},parseEl:function(b){var c=n.items[b],d;c.tagName?c={el:a(c)}:(d=c.type,c={data:c,src:c.src});if(c.el){var e=n.types;for(var f=0;f<e.length;f++)if(c.el.hasClass("mfp-"+e[f])){d=e[f];break}c.src=c.el.attr("data-mfp-src"),c.src||(c.src=c.el.attr("href"))}return c.type=d||n.st.type||"inline",c.index=b,c.parsed=!0,n.items[b]=c,y("ElementParse",c),n.items[b]},addGroup:function(a,b){var c=function(c){c.mfpEl=this,n._openClick(c,a,b)};b||(b={});var d="click.magnificPopup";b.mainEl=a,b.items?(b.isObj=!0,a.off(d).on(d,c)):(b.isObj=!1,b.delegate?a.off(d).on(d,b.delegate,c):(b.items=a,a.off(d).on(d,c)))},_openClick:function(b,c,d){var e=d.midClick!==undefined?d.midClick:a.magnificPopup.defaults.midClick;if(!e&&(b.which===2||b.ctrlKey||b.metaKey||b.altKey||b.shiftKey))return;var f=d.disableOn!==undefined?d.disableOn:a.magnificPopup.defaults.disableOn;if(f)if(a.isFunction(f)){if(!f.call(n))return!0}else if(r.width()<f)return!0;b.type&&(b.preventDefault(),n.isOpen&&b.stopPropagation()),d.el=a(b.mfpEl),d.delegate&&(d.items=c.find(d.delegate)),n.open(d)},updateStatus:function(a,b){if(n.preloader){q!==a&&n.container.removeClass("mfp-s-"+q),!b&&a==="loading"&&(b=n.st.tLoading);var c={status:a,text:b};y("UpdateStatus",c),a=c.status,b=c.text,n.preloader.html(b),n.preloader.find("a").on("click",function(a){a.stopImmediatePropagation()}),n.container.addClass("mfp-s-"+a),q=a}},_checkIfClose:function(b){if(a(b).hasClass(m))return;var c=n.st.closeOnContentClick,d=n.st.closeOnBgClick;if(c&&d)return!0;if(!n.content||a(b).hasClass("mfp-close")||n.preloader&&b===n.preloader[0])return!0;if(b!==n.content[0]&&!a.contains(n.content[0],b)){if(d&&a.contains(document,b))return!0}else if(c)return!0;return!1},_addClassToMFP:function(a){n.bgOverlay.addClass(a),n.wrap.addClass(a)},_removeClassFromMFP:function(a){this.bgOverlay.removeClass(a),n.wrap.removeClass(a)},_hasScrollBar:function(a){return(n.isIE7?s.height():document.body.scrollHeight)>(a||r.height())},_setFocus:function(){(n.st.focus?n.content.find(n.st.focus).eq(0):n.wrap).focus()},_onFocusIn:function(b){if(b.target!==n.wrap[0]&&!a.contains(n.wrap[0],b.target))return n._setFocus(),!1},_parseMarkup:function(b,c,d){var e;d.data&&(c=a.extend(d.data,c)),y(f,[b,c,d]),a.each(c,function(c,d){if(d===undefined||d===!1)return!0;e=c.split("_");if(e.length>1){var f=b.find(j+"-"+e[0]);if(f.length>0){var g=e[1];g==="replaceWith"?f[0]!==d[0]&&f.replaceWith(d):g==="img"?f.is("img")?f.attr("src",d):f.replaceWith(a("<img>").attr("src",d).attr("class",f.attr("class"))):f.attr(e[1],d)}}else b.find(j+"-"+c).html(d)})},_getScrollbarSize:function(){if(n.scrollbarSize===undefined){var a=document.createElement("div");a.style.cssText="width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;",document.body.appendChild(a),n.scrollbarSize=a.offsetWidth-a.clientWidth,document.body.removeChild(a)}return n.scrollbarSize}},a.magnificPopup={instance:null,proto:o.prototype,modules:[],open:function(b,c){return A(),b?b=a.extend(!0,{},b):b={},b.isObj=!0,b.index=c||0,this.instance.open(b)},close:function(){return a.magnificPopup.instance&&a.magnificPopup.instance.close()},registerModule:function(b,c){c.options&&(a.magnificPopup.defaults[b]=c.options),a.extend(this.proto,c.proto),this.modules.push(b)},defaults:{disableOn:0,key:null,midClick:!1,mainClass:"",preloader:!0,focus:"",closeOnContentClick:!1,closeOnBgClick:!0,closeBtnInside:!0,showCloseBtn:!0,enableEscapeKey:!0,modal:!1,alignTop:!1,removalDelay:0,prependTo:null,fixedContentPos:"auto",fixedBgPos:"auto",overflowY:"auto",closeMarkup:'<button title="%title%" type="button" class="mfp-close">×</button>',tClose:"Close (Esc)",tLoading:"Loading...",autoFocusLast:!0}},a.fn.magnificPopup=function(b){A();var c=a(this);if(typeof b=="string")if(b==="open"){var d,e=p?c.data("magnificPopup"):c[0].magnificPopup,f=parseInt(arguments[1],10)||0;e.items?d=e.items[f]:(d=c,e.delegate&&(d=d.find(e.delegate)),d=d.eq(f)),n._openClick({mfpEl:d},c,e)}else n.isOpen&&n[b].apply(n,Array.prototype.slice.call(arguments,1));else b=a.extend(!0,{},b),p?c.data("magnificPopup",b):c[0].magnificPopup=b,n.addGroup(c,b);return c};var C="inline",D,E,F,G=function(){F&&(E.after(F.addClass(D)).detach(),F=null)};a.magnificPopup.registerModule(C,{options:{hiddenClass:"hide",markup:"",tNotFound:"Content not found"},proto:{initInline:function(){n.types.push(C),w(b+"."+C,function(){G()})},getInline:function(b,c){G();if(b.src){var d=n.st.inline,e=a(b.src);if(e.length){var f=e[0].parentNode;f&&f.tagName&&(E||(D=d.hiddenClass,E=x(D),D="mfp-"+D),F=e.after(E).detach().removeClass(D)),n.updateStatus("ready")}else n.updateStatus("error",d.tNotFound),e=a("<div>");return b.inlineElement=e,e}return n.updateStatus("ready"),n._parseMarkup(c,{},b),c}}});var H,I=function(){return H===undefined&&(H=document.createElement("p").style.MozTransform!==undefined),H};a.magnificPopup.registerModule("zoom",{options:{enabled:!1,easing:"ease-in-out",duration:300,opener:function(a){return a.is("img")?a:a.find("img")}},proto:{initZoom:function(){var a=n.st.zoom,d=".zoom",e;if(!a.enabled||!n.supportsTransition)return;var f=a.duration,g=function(b){var c=b.clone().removeAttr("style").removeAttr("class").addClass("mfp-animated-image"),d="all "+a.duration/1e3+"s "+a.easing,e={position:"fixed",zIndex:9999,left:0,top:0,"-webkit-backface-visibility":"hidden"},f="transition";return e["-webkit-"+f]=e["-moz-"+f]=e["-o-"+f]=e[f]=d,c.css(e),c},h=function(){n.content.css("visibility","visible")},i,j;w("BuildControls"+d,function(){if(n._allowZoom()){clearTimeout(i),n.content.css("visibility","hidden"),e=n._getItemToZoom();if(!e){h();return}j=g(e),j.css(n._getOffset()),n.wrap.append(j),i=setTimeout(function(){j.css(n._getOffset(!0)),i=setTimeout(function(){h(),setTimeout(function(){j.remove(),e=j=null,y("ZoomAnimationEnded")},16)},f)},16)}}),w(c+d,function(){if(n._allowZoom()){clearTimeout(i),n.st.removalDelay=f;if(!e){e=n._getItemToZoom();if(!e)return;j=g(e)}j.css(n._getOffset(!0)),n.wrap.append(j),n.content.css("visibility","hidden"),setTimeout(function(){j.css(n._getOffset())},16)}}),w(b+d,function(){n._allowZoom()&&(h(),j&&j.remove(),e=null)})},_allowZoom:function(){return n.currItem.type==="image"},_getItemToZoom:function(){return n.currItem.hasSize?n.currItem.img:!1},_getOffset:function(b){var c;b?c=n.currItem.img:c=n.st.zoom.opener(n.currItem.el||n.currItem);var d=c.offset(),e=parseInt(c.css("padding-top"),10),f=parseInt(c.css("padding-bottom"),10);d.top-=a(window).scrollTop()-e;var g={width:c.width(),height:(p?c.innerHeight():c[0].offsetHeight)-f-e};return I()?g["-moz-transform"]=g.transform="translate("+d.left+"px,"+d.top+"px)":(g.left=d.left,g.top=d.top),g}}}),A()})
/* Magnific Popup CSS */
.mfp-bg {
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1042;
overflow: hidden;
position: fixed;
background: #0b0b0b;
opacity: 0.8; }
.mfp-wrap {
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1043;
position: fixed;
outline: none !important;
-webkit-backface-visibility: hidden; }
.mfp-hide {
display: none !important; }
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!doctype html>
<head>
<style>
.div_show {
height: 33px; width: 198px; background-color: rgb(0, 123, 209);
position: absolute;
transition: bottom .3s;
bottom: -33px;
}
.hoverside:hover .div_show{
bottom: 0px;
}
.hoverside {
background-color: rgb(255, 255, 255);
width: 200px;
height: 150px;
border-radius: 3px;
border: 1px solid #dddddd;
float: left;
position: relative;
overflow: hidden;
}
.popup{
width: 400px;
height:150px;
background-color: #f5f5f5;
position: absolute;
bottom: 40%;
left: 50%;
}
</style>
</head>
<body>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.popup-content').magnificPopup({
type: 'inline'
});
});
</script>
<div>
<div class="hoverside">
<a href="#popup" class="popup-content">
<div class="div_show">
<p class="link">Click</p>
</div>
</a>
<div id="popup" class="popup mfp-hide">
<h3>Popup</h3>
</div>
</div>
</div>
Point to the area, the blue div appears. Make a click. A pop-up window appears and the blue div disappears. Ok, that's what I need. But when you close the pop-up window, the blue div will reappear. How to fix it?
use the default plugin css. after that just active the plugin. then do cuatomize what you need.
here are the repo http://dimsemenov.com/plugins/magnific-popup/
If you set to autoFocusLast: true last focused element before popup show up will be focused after popup close. You can set autoFocusLast: false for hiding the last focused element.
Example demo: https://codepen.io/rubel/pen/ZZQved
See more information: https://dimsemenov.com/plugins/magnific-popup/documentation.html#autofocuslast
Related
Problem hiding a click-triggered div when anywhere on the page is clicked while still keeping everything selectable
Say a menu is triggered when a button is clicked, now 1_ For canceling it, the user has to be able to click anywhere on the page (not only on the same button), 2_ Everything else on the page must still remain selectable throughout this process. Here's what I've tried: $(".dad").click(function() { $(".son").show(); $(".mask").show(); }); $(".mask").click(function() { $(".son").hide(); $(".mask").hide(); }); .dad { background: greenyellow; width: 20px; height: 20px; margin-top: 100px; z-index: 2; } .son { position: relative; left: 20px; bottom: 100px; width: 100px; height: 100px; display: none; background: tomato; z-index: 2; } p { z-index: 2; } .mask { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; display: none; } <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js "></script> <p>This is a paragraph</p> <div class="dad"> <div class="son"></div> </div> <div class="uncle"></div> <div class="mask"></div> This code satisfies the first condition(the ".son" hides when anywhere on the page is clicked), but the second condition isn't met. Because when the ".son" is visible, the paragraph is not immediately selectable, unless the user does another click. Although this seems like a minor problem, sometimes it can become a little annoying, thus is a requirement. (I've also tried other ways. E.g. CSS "pointer-events: none" on the mask, but it has a different purpose, because it also cancels click events). So how could it be done? Thanks in advance. Note: This is not solely a CSS question, I embrace any Javascript or Jquery answers too should they give easier/better solutions.
Hope it helpful... $(".dad").click(function() { $(".son").show(); }); $(document).click(function (e) { var container = $(".dad"); if(!container.is(e.target) && container.has(e.target).length === 0) { $(".son").hide(); } }); .dad { background: greenyellow; width: 20px; height: 20px; margin-top: 100px; z-index: 2; } .son { position: relative; left: 20px; bottom: 100px; width: 100px; height: 100px; display: none; background: tomato; z-index: 2; } p { z-index: 2; } <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js "></script> <p>This is a paragraph</p> <div class="dad"> <div class="son"></div> </div> <div class="uncle"></div>
Changing the display from block to none, Problems in showing div
This is my first question here and I've tried to search for quite some time now and I haven't found any question that is the same as mine or touches the same problem. I want to do a CSS popup that has a background-div covering the whole website and a centered div showing actual content. My problem is that only the centered div is showing up when I'm clicking the button that is supposed to show them both. Even when I comment out the display:none - attribute in my css-file, the background div simply doesn't have any color or style attached to it. If I fill it with text, the text shows up on the website where the div is "supposed" to be if there weren't any style sheet attached to it. I've gotten the code from coders answer in this question. Here it is: Script: $("#btn-update").click(function() { document.getElementById("light").style.display="block"; document.getElementById("blackground").style.display="block"; }); html: <button type="button" id="btn-update">Button!</button> <div id="light" class="white_content">This is the lightbox content. Close</div> <div id="blackground" class="black_overlay"></div> CSS: .black_overlay{ /*display: none;*/ position: absolute; top: 10%; left: 10%; width: 100%; height: 100%; background-color: #000000; z-index:1001; /*-moz-opacity: 0.8;*/ /*opacity:.80;*/ /*filter: alpha(opacity=80);*/ } .white_content { display: none; position: absolute; top: 25%; left: 25%; width: 15%; height: 15%; padding: 16px; border: 16px solid orange; background-color: white; z-index:1002; overflow: auto; } Here's the fiddle-demo so you can play around as well I've tried changing the attributes, commenting them out, making the div visible from the get go but it always seems to not show properly (while the white_content always do). Also: the JS-fiddle is having problems showing the white content, but the black overlay is showing just fine when you remove the display:none attribute. Thank you so much in advance for any help. Been stuck for a while now
You need to attach the jquery plugin in jsfiddle http://jsfiddle.net/dhana36/K57DH/12/ After update http://jsfiddle.net/dhana36/K57DH/20/ UPDATE: HTML CODE: <!DOCTYPE html> <html> <head> <style> .black_overlay{ /* display: none;*/ position: absolute; top: 10%; left: 10%; width: 50%; height: 50%; background-color: #000000; z-index:1001; /* -moz-opacity: 0.8;*/ /* opacity:.80;*/ /* filter: alpha(opacity=80);*/ } .white_content { display: none; position: absolute; top: 25%; left: 25%; width: 15%; height: 15%; padding: 16px; border: 16px solid orange; background-color: white; z-index:1002; overflow: auto; } </style> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#btn-update").click(function() { document.getElementById("light").style.display="block"; document.getElementById("blackground").style.display="none"; //document.getElementById("blackground").style.background-color="#555555"; }); }); </script> </head> <body> <div id="light" class="white_content"> This is the lightbox content. <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='none';document.getElementById('blackground').style.display='block'"> Close </a> </div> <div id="blackground" class="black_overlay"></div> <button type="button" id="btn-update">Button!</button> </body> </html>
Add the script before closing </body> not inside </head> Same code doesn't work when wrapped inside head http://jsfiddle.net/K57DH/18/ edit in left panel
div popup not working
What I am doing wrong? When you click on class divtop, it should show a div popup in the middle of the page. At that time back page should become not clickable. escape or a button in popup will close it. <html lang="en" class=" en"> <head> <title>My Test Popup</title> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <style type="text/css"> .divtop { width: 800px; height: 300px; border:solid; } .divbottom { top: 400px; } .localmenu { border: 1px solid black; background: #fff; margin-left : auto; top: 50px; width: 300px; padding-top: 25px; margin-top: 100px; height: 150px; } .appContent{ width: 800px; border:solid; height: 600px; display: block; margin-left: auto; margin-right: auto; } .maincontent{ width: 100%; } </style> </head> <body> <div class="appContent" > <div class="maincontent" > <div class="divtop" >Top</div> <div class="divtop divbottom" >Bottom</div> </div> <div id="popup" style="width : 100%; height: 600px;display: none;"> <div class='localmenu'> Text in Div Popup<br/> <button id="btnHide">Close</button><br/> </div> </div> </div> <script> $(document).ready(function() { $('.divtop').click(function() { $('#popup').show().css("top", "500px").animate({top: 50}, 200); $('.mainContent').css("background-color", "grey"); }); $('#btnHide').click(function() { $('#popup').hide(); }); }); </script> </body> </html>
Fiddle I added some CSS to your #popup and it's now all in the CSS (not inline in the html). Changed also your jQuery animate to 50px, instead of just 50. I think you have small adjustments to do to the CSS, like in .localmenu I'm not sure why you have both padding-top: 25px; margin-top: 100px;. CSS #popup { position:absolute; display: none; float: left; left:30%; z-index:1; } #popoverlay { position: fixed; display:none; top: 0; left: 0; width: 100%; height: 100%; background-color: #000; opacity: 0.5; } jQuery $(document).ready(function () { $('.divtop').click(function () { $('#popoverlay').show(); $('#popup').show().css("top", "500px").animate({ top: "50px" }, 200); $('.mainContent').css("background-color", "grey"); }); $('#btnHide').click(function () { $('#popup').hide(); $('#popoverlay').hide(); }); }); HTML <div class="appContent"> <div class="maincontent"> <div class="divtop">Top</div> <div class="divtop divbottom">Bottom</div> </div> <div id="popup"> <div class='localmenu'>Text in Div Popup <br/> <button id="btnHide">Close</button> <br/> </div> </div> </div>
To get it to work properly, even if there is a vertical scroll bar, you have to use position "fixed". Place popup as a direct child of body and make it's position: fixed, and width and height 100%. Place localmenu as a direct child of body as well. Working example at jsbin. Html: <div id="popup"> <!--// This is to stop the user from interacting with the content in the back // and to give a visual clue about that --> </div> <div class='localmenu'> <div> Text in Div Popup<br/> <button id="btnHide">Close</button><br/> </div> </div> <div class="appContent" > <div class="maincontent" > <div class="divtop" >Top</div> <div class="divtop divbottom" >Bottom</div> </div> </div> CSS: //Use opacity to give a visual clue. Please note that this doesn't work in -all- browsers #popup { position: fixed; width: 100%; height: 100%; display: none; background: black; opacity: .5; top: 0; left: 0; } //This is just to be able to center the actual menu .localmenu { top: 20%; left: 0; width: 100%; position: fixed; height: 150px; display: none; } .localmenu > div { border: 1px solid blue; background: #fff; margin-left : auto; margin-right: auto; width: 300px; height: 150px; } Javascript: (This is mostly the same, although I removed the animate, because I don't know exactly how it works and it needs to end at 'top: 0'. As localmenu and popup are seperate, we show them seperate as well.) $(document).ready(function() { $('.divtop').click(function() { $('#popup').show().animate(200); $('.localmenu').show(); //$('.mainContent').css("background-color", "grey"); }); $('#btnHide').click(function() { $('#popup').hide(); $('.localmenu').hide(); }); });
To block the div tags at the back from being clickable: Add a div with the following style in your HTML. Im gonna call it overlay. .overlay { width: 100%; height: 100%; background-color: #000; left: 0; opacity: .8; position: absolute; top: 0; z-index: 10000; display: none; } This will essentially cover up your page when shown up. To center your popup: I added some extra styles to #popup and removed some from .localmenu. You were missing position: absolute and z-index, added those in. (z-index of popup must be > z-index of overlay) #popup { background: #fff; position :absolute; left : 40%; width : 300px; height: 600px; height: 150px; display: none; z-index: 10001; } .localmenu { border: 1px solid black; } Then, in your JS, In your animate method, I changed 50px to 30% to center div#popup Added code to hide and show .overlay along with #popup. After the changes, $(document).ready(function () { $('.divtop').click(function () { $('#popup').show().css("top", "500px").animate({ top: "30%" }, 200); $('.overlay').show(); }); $('#btnHide').click(function () { $('#popup,.overlay').hide(); }); }); Demo http://jsbin.com/olasog/1 Code http://jsbin.com/olasog/1/edit
Try this: $(document).ready(function() { $('.divtop').click(function() { var div = $('.appContent'); $('.localmenu').css({'margin': '200px auto'}); $('#popup').show().css({top: "500px", position: 'absolute', width: div.width(), height: div.height()}).animate({top: 0}, 200); $('.mainContent').css("background-color", "grey"); }); $('#btnHide').click(function() { $('.mainContent').css("background-color", ""); $('#popup').hide(); }); });
Kendo UI - Why can't I open and close properly a window?
I've tried different methods to open my window, but so far everything fails. My first try was to open the window when a button was clicked with a code like this based on this example: $('#btnUsuarios').click(function (e) { onUsuariosLoad(); var window = $('#usuariosDiv'); if (!window.data('kendoWindow')) { window.kendoWindow({ draggable: true, height: "300px", modal: true, resizable: false, title: "Modal Window", width: "65%", close: hideUsuarios }); } else { window.data('kendoWindow').open(); } hideHomeScreen(); showUsuarios(); }); onUsuariosLoad is a function that loads the content for the window from a remote html, the function makes sure to only load once the content, if it had been loaded already then it doesn't load it again, I've tested the function before trying to implement the window Kendo UI widget to make sure it works properly. hideHomeScreen is a function that hides some element from the div that open the window. showUsuarios is a function that shows the content for the window. It was supposed to check if the data for a kendoWindow had been created then to open the window, else it should create it. The first time I click the button it works, but when I close the window and click again the button, the window isn't show at all (I'm talking about the window widget UI, not the contents of the usuariosDiv, I checked Firebug and the contents for usuariosDiv are still there, but the window widget UI isn't shown). The second try I did was like this: $('#btnUsuarios').click(function (e) { onUsuariosLoad(); $('#usuariosDiv').kendoWindow({ draggable: true, height: "300px", modal: true, resizable: false, title: "Modal Window", width: "65%", close : hideUsuarios }); hideHomeScreen(); showUsuarios(); }); This is similar to the first attemp but it happens the same thing: it works fine the first time, but when I close the window it fails to reopen the window when I click again the button. My third try was based on the example Initialize Window, center, and configure button click action from this page: var win = $("#usuariosDiv").kendoWindow({ draggable: true, height: "300px", modal: true, resizable: false, visible: false, title: "Modal Window", width: "65%", close: hideUsuarios }).data("kendoWindow"); $('#btnUsuarios').click(function (e) { onUsuariosLoad(); var win = $("#usuariosDiv").data("kendoWindow"); win.open(); hideHomeScreen(); showUsuarios(); }); This doesn't show the window properly, instead of showing a window with the specified dimensions, it only shows a tiny dot modal window. That's for the opening part, my problem with closing the window comes when I try to do it by an event triggered by a control on the window. I'm not talking about the close button on the upper right corner, I'm refering to any button I have displayed on the window. For example, I tried this to close the window: $('#btnBack').click(function (e) { hideUsuarios(); var window = $('#usuariosDiv'); window.data('kendoWindow').close(); }); But I get the following error: Uncaught TypeError: Cannot call method 'close' of undefined. If you're asking why I don't simply rely on the default close button on the window, it's because there're procedures, that when finished, need to close the window. This is my View: <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link href="<%: Url.Content("~/Content/kendo/2012.3.1114/kendo.common.min.css")%>" rel="stylesheet" type="text/css" /> <link href="<%: Url.Content("~/Content/kendo/2012.3.1114/kendo.metro.min.css")%>" rel="stylesheet" type="text/css" /> <script src="http://code.jquery.com/jquery-1.7.min.js" type="text/javascript"></script> <script src="<%: Url.Content("~/Scripts/kendo/2012.3.1114/kendo.web.min.js")%>"></script> <script src="<%: Url.Content("~/Scripts/jquery.signalR-1.0.0-rc1.min.js")%>" type="text/javascript"></script> <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3" type ="text/javascript" ></script> <script src="<%: Url.Content("~/Scripts/index/templates.js")%>" type="text/javascript"></script> <script src="<%: Url.Content("~/Scripts/index/main.js")%>" type="text/javascript"></script> <style type="text/css"> #verticalMenu { top: 39px; left: 0px; } </style> </head> <body onload="onPageLoad()"> <div id="canvasDiv" style="position: absolute; top: 0px; left: 0; width: 100%; height: 100%; background-color: transparent;"> <div id="alertDiv" style="position: absolute; top: -250px; background-color: #F00; z-index: 50; opacity:1; border-top: 1px solid #FFF; border-right: 1px solid #F5F5F5; border-bottom: 1px solid #D5D5D5; border-left: 1px solid #F5F5F5"> <div id="msgDiv" style="position:absolute;left:10px; right:10px; bottom: 10px; text-align: center;width: 50%; margin: 0px auto;">Alerta Sismo Detectado</div> <div id="btnCloseAlert" style="position: absolute; top: 0px; right: 0"><span class="k-icon k-i-close"></span></div> </div> <div id="homeScreenDiv" style="position: absolute; top: 0px; left: 0; width: 100%; height: 100%; background-color: white; z-index: 10; opacity:1"> </div> <div id="usuariosDiv" style="position: absolute; left: 0; width: 100%; height: 100%; background-color: white; z-index: 2; opacity:0"> </div> <div id="sismosDiv" style="position: absolute; left: 0; width: 100%; height: 100%; background-color: white; z-index: 2; opacity:0"> <div id="leftBar" style="border: thin solid #666666; position: absolute; top: 0px; left: 0; width: 190px; height: 100%; background-color: white; z-index:20"> <div onclick="hideSismos()" style="position:absolute;height: 39px; width: 25%; left:0; top:0"><div style="position:absolute; top: 10px; bottom:10px;left:10px;right:10px"><span class="k-icon k-i-arrow-w"></span></div></div> <div style="position:absolute;height: 39px; width: 75%; right:0; top:10px; bottom:10px; text-align:center">Sismos</div> <ul id="verticalMenu" style="position: absolute; width: 99%; height: auto; display: block;" > <li id="Regiones" style="height: 75px; text-align: center;">Regiones</li> <li id="Clusters" style="height: 75px; text-align: center;">Clusters</li> <li id="Dispositivos" style="height: 75px; top: auto; text-align: center;">Dispositivos</li> <li id="Eventos" style="height: 75px; text-align: center;">Eventos</li> </ul> </div> <div id="selectionBar" style="border: thin solid #666666; position: absolute; top: 0px; left: 0; width: 180px; height: 100%; background-color: white; left: 10px; overflow:auto; z-index:9"> <div class="k-toolbar k-grid-toolbar" style="height: 34px;"> <button id="btnAgregarEntry" class="k-button k-button-icontext k-add-button" onclick="swapToNewData(this)"><span class="k-icon k-add"></span>Agregar</button> </div> <div id="listView" style="background-color: transparent; position: absolute; top: 40px; left: 0px; right: 0px; width:auto; height:auto" ></div> </div> <div id="dataBar" style="border: thin solid #666666; position: absolute; top: 0px; left: 0; width: 330px; height: 100%; background-color: white; left: 10px; overflow:auto; z-index:8"> <div id="dataView" style="background-color: transparent; position: absolute; top: 40px; bottom:0px; left: 0px; right: 0px; width:auto; height:auto" ></div> </div> <div id='myMap' style="border: thin solid black; background-color: white; position: absolute; display: block; min-height: 100%; top: 0px; left: 189px; width: 88%; z-index: 20"></div> </div> </div> </body> </html> Can anyone help me to understand what am I doing wrong?
I had a similar problem and instead of close() I suggest using destroy() and creating new window every time.
I would prefer to bind the destroy-method to the deactivate-event and use the close()-method on the window. This way, the closing-animation will be properly finished. ... deactivate: function() { this.destroy(); } ...
I realise this is long after the original post but the 70% scale factor is set in the window close options. The bug seems to affect opening and closing the same window, as the window gets scaled to 70% on close but never put to 100% on open. I worked around this by adding the following to stop it scaling it to 70% on close. var emptyWindow = $(theWindowId).data("kendoWindow"); emptyWindow.options.animation.close.effects.zoom.properties.scale = 1;
Insted win.open(); you use below code. $("#usuariosDiv").data("kendoWindow").open();
This should work to close a window: $("#btnBack").click( function (e) { $("#usuariosDiv").data("kendoWindow").close(); }); Also you may want to look into setting animation:false; on the window open. I remember I had some similar problems that were associated to that. As far as your first attempt you may want to try this (added .data("kendowindow") into the first creation) $("#btnUsuarios").click(function (e) { onUsuariosLoad(); var window = $("#usuariosDiv"); if (!window.data("kendoWindow")) { window.kendoWindow({ draggable: true, height: "300px", modal: true, resizable: false, title: "Modal Window", width: "65%", close: hideUsuarios }).data("kendoWindow"); } else { window.data("kendoWindow").open(); } hideHomeScreen(); showUsuarios(); });
onclick, how can I hide a <div> and show the content behind it?
How can I make this <div> hide itself when a click occurs outside of it? I would like to be able to hide it and then show the rest of the content of the page that is behind it. <html> <style> .overlay { position: fixed; top: 0; left: 0; height: 100%; width: 100%; background: #fff; opacity: 0.5; filter: alpha(opacity=50); z-index: 80; } .popupContent { background: #fff; padding: 5px; border: 1px solid black; position: fixed; top: 100px; left: 400px; bottom: 365px; right: 300px; z-index: 81; } </style> <div id="initialPopup"> <div class="overlay"></div> <div class="popupContent" onclick="hideInitialPopup()"> Good Morning, Please Upload Your File Today! </div> </div> <script> function hideInitialPopup() { document.getElementById("initialPopup").style.display = "block"; $("popupContent").mouseup(function(){ if(! mouse_is_inside) $ } </script> </html>
Use jQuery's event.target to determine if you've clicked within the box or not: $(document).on('click',function(e) { if (!$(e.target).closest('#my_div_id').length) { $('#my_div_id').hide(); } }); http://jsfiddle.net/mblase75/PNYTX/
Usually when you use a mask with something like a popup/dialog, it's easiest just to attach a click handler to the mask itself that hides the popup/dialog when clicked. Here is a jfiddle: http://jsfiddle.net/peterdev001/SDNUj/