Tag: jquery

Closable jquery full screen popup window and how to make it using CSS

Have you ever wondered how some websites display a closable div or a full screen overlay on top of their main content to say something really important to the viewer? I had to do something similar for my old portfolio that is no longer updated, to direct readers to my new and better looking portfolio instead. Here’s how I did it.

HTML popup window is dead

I just had to say this before I continue. Do not use HTML/Javascript pop up windows- most browsers block these by default and if you mange to bypass the popup blockers, you’re just going to annoy your readers so much they will leave and never come back. Seriously, don’t do it! Instead, use a more modern approach by making a jquery popup box.

Full screen overlay effect in CSS

Consider a simple container div. We need to make its height and width match our viewport/screen. We can do this by applying a 100% viewport width and height:

#box {
height:100vh;
width:100vw;
background:white;
}

Depending on what padding/margin rules you have set for your body element, you might see that the box is not fully covering the screen yet. Besides, we need to position it outside the normal flow anyway, because it needs to stay on top of everything:

#box {
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
}

Now it should cover the whole screen with no margin anywhere. But is it on top of your main content? Probably not. You need to add a high enough z-index to it so it will stay on top:

#box {
z-index:999;
}

But wait… now you just have a box covering the whole screen and your content cannot be accessed. We need to have the box initially hidden so we can make it visible using jquery later on:

#box {
display:none;
}

Right now when you load it up, nothing should show because we have hidden the box. It’s advised to only load the overlay div when the whole page has finished loading so we will put the call inside a document .ready function using jquery:

<script>
$(document).ready(function(){
  $( "#box" ).fadeIn( "slow", function() {
    //Box loaded
  });
});
</script>

The box should now load only when the rest of your website has loaded first. So far so good, but there is no way for your user to close the box. To do this, we need another div (it can contain a cool icon if you wish). I’m using a simple X icon with a transparent background found on Google:

<div id="close">
    <img src="http://ynef.net/portfolio/src/close-button.png" alt="Close" height="64" width="64"/>
</div>

The close button would look stupid if it was loaded immediately so we need to hide it the same way we hid the box itself in CSS:

#close {
display:none;
}

While we’re at it, we should position it in a place where it makes sense too. I suggest putting it in the top right corner of your screen with 10px away from both the top and right side (the display:block will make the entire div clickable):

#close {
position:absolute;
top:10px;
right:10px;
display:block;
}

Finally don’t forget to add a higher z-index than what you had for the box. In our case a z-index of 1000 should do the trick:

#close {
z-index:1000;
}

Back to jquery! Since the close button is hidden at first, we need to make it visible after we have loaded the box. Let’s also add a .click function to the close button that will hide everything when clicked:

<script>
$(document).ready(function(){
  $( "#box" ).fadeIn( "slow", function() {
        $( "#close" ).show();
        $( "#close" ).click(function() {
        $( "#box" ).hide();
        $( "#close" ).hide();
    });
  });
});
</script>

Now that we have covered everything that’s happening, I can let you copy and paste the whole code as one:

CSS for fullscreen div

#box {
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
z-index:999;
display:none;
}

#close {
display:none;
position:absolute;
top:10px;
right:10px;
display:block;
z-index:1000;
}

And the jquery

$(document).ready(function(){
  $( "#box" ).fadeIn( "slow", function() {
        $( "#close" ).show();
        $( "#close" ).click(function() {
        $( "#box" ).hide();
        $( "#close" ).hide();
    });
  });
});
Share!

WordPress Portfolio Theme

By breaking up the skeleton of underscores I was able to craft this portfolio theme for WordPress

Screenshot of ynefs portfolio

Main features

Navigation menu is generated from categories and displays a category view

Cool sliding effect on the menu using a combination of transition and padding

As you scroll down the site title and description disappear along with the navigation menu

They reappear when you start scrolling up

Custom header gets added to Artwork and Music categories by manipulating directly CSS style with jQuery

Footer has three distinct columns

Picture links in gallery and footer icons have a tint overlay effect on hover

 

Share!

How to make a closable hover header div for your website announcements or ads using CSS and Javascript

html and css programming language pictureIt’s perfect for site news

In this tutorial you will learn how to make a “hovering” header for your website news, announcements or ads. I am currently using the same technique on this blog too and it should have loaded up by now. (not any more!) As you can see it can be easily closed by the red X button and it’s “hovering” over the main content so it keeps showing even when your users scroll down.

The requirements

This script uses Font Awesome and jQuery via CDN. This basically just means that you need to add the following code inside your <head></head> tags:

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

The HTML and CSS parts

This will be the face for your news box and you can customize things like font size, background color, padding, etc. Add the following code right after your opening <body> tag:

<div id="announcement">
 <span id='message'>Your message goes here. You can add a link if you want or whatever you like.</span>
 <span id='close'><a href="#" title="Close"><i class='fa fa-times'></i></a></span>
 <div id="delimiter"></div>
 </div>

* I’m using the delimeter div to clear both floats so it will not cause any styling errors.

Here is my CSS

Customize it to fit the design of your website!

#announcement {
    padding: 0;
    margin: 0;
    display: none;
    background: #CCFF99;
    height: auto;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
 z-index: 9999;
}
#message {color: #000; font-size: 1em; float: left; padding: 0 2.1% 0 2.1%;}
#close {position: fixed; top: 0; right: 0; padding: 0 2.1% 0 2.1%;}
.fa-times {color: red;}
#delimiter { clear: both; }

* The position: fixed; along with top: 0; and left: 0; are what make the div hover over the main content. Same goes for the close button- just replace left with right to put it on the top right side of your page.

* z-index is a useful property because it specifies the stack order of an element. By giving it an unrealistic value we make sure it will stay on top of every possible element on your page.

* I’m using display: none; so that it will only display after Javascript has determined that the page is fully loaded.

* The fa-times class is for our close button. Font Awesome is really awesome ;)

Finally the Javascript

This is what makes everything work nicely! Add this code inside your <head></head> tags:

<script>
 window.onload = function () {
 document.getElementById('close').onclick = function(){
 this.parentNode.parentNode.removeChild(this.parentNode);
 return false;
 };
 }
 $(document).ready(function() {
 $("#announcement").delay(2000).fadeIn(500);
 });
 </script>

* I use the window.onload function to execute the script only after the whole page has finished loading. If your user is waiting for the page to load and at the same time a news box appears, it might scare away a potential client.

* You should also notice the getElementById(‘close’) and $(“#announcement”). Do not change these, they are the ID’s of the close button and the news box itself.

Share!