Monday, November 30, 2015

Pop a Message Box in the Center with Freezing and Hiding the Scrollbar

This is the continuation of the previous post with also the codes to freeze the mouse scroll and hide the scroll bar. This is crucial to lock on a page to avoid any scrolling events after a message box popped up to demand a better attention from the visitor. Basically the codes are just added with a the scroll-locking codes I posted earlier here. In addition to that, I included the scrollbar disabling javascript "document.body.style.overflowY = 'hidden';" code. Here are the long combined codes:

<!DOCTYPE HTML>
<html>

<style type="text/css">

#curtain {
left:0px;
top:0px;
filter:alpha(opacity=70);
-moz-opacity:0.7;
opacity:0.7;
width: 100%;
height: 100%;

}

#message_box {
left:50%;
top:50%;
width: 200px;
height: 70px;
z-index: 1;
margin-top: -35px;
margin-left: -100px;
}

div > div#curtain { position: fixed; }
div > div#message_box { position: fixed; }

</style>

<script>

var ff = (navigator.userAgent.indexOf("Firefox") != -1);

if (ff) {
 mousewheelevent = "DOMMouseScroll";
}
else {
 mousewheelevent = "mousewheel";
}

function lockScroll() {
 if (document.attachEvent) {
  document.attachEvent("on"+mousewheelevent, catchWheel);
 }
 else if (document.addEventListener) {
  document.addEventListener(mousewheelevent, catchWheel, false);
 }

}

function unlockScroll() {
 if (document.detachEvent) {
  document.detachEvent("on"+mousewheelevent, catchWheel);
 }
 else if (document.removeEventListener) {
  document.removeEventListener(mousewheelevent, catchWheel, false);
 }

}


function catchWheel(e){

 if (e.preventDefault) {
  e.preventDefault();
 }
 else {
  e.returnValue = false;
 }
}


lockScroll(); // to lock scroll
unlockScroll(); // to unlock scroll


function close() {
 document.body.style.overflowY = 'scroll';
 document.getElementById('curtain').style.visibility = "hidden";
 document.getElementById('message_box').style.visibility = "hidden";
 unlockScroll();
}

function open() {
 document.body.style.overflowY = 'hidden';
 document.getElementById('curtain').style.visibility = "visible";
 document.getElementById('message_box').style.visibility = "visible";
 lockScroll();
}


lockScroll();

</script>


<body bgcolor="#aabbcc">


<div style="width:100%;height:100%;">

<div id="message_box" style="border:1px solid #333; background-color:#ddd; text-align:center; padding:10px; ">
<p>Message Here<br><a href="javascript:close()">Close</a></p>
</div>

<div id="curtain" style="background-color:#ddd;"> </div>

</div>


<table width="95%" align=center><tr><td>
<br><a href="javascript:open()">Show Message Box</a>
<br>line 1<br>line 2<br>line 3<br>line 4<br>line 5<br>line 6<br>line 7<br>line 8<br>line 9<br>line 10
<br>line 11<br>line 12<br>line 13<br>line 14<br>line 15<br>line 16<br>line 17<br>line 18<br>line 19<br>line 20
<br>line 21<br>line 22<br>line 23<br>line 24<br>line 25<br>line 26<br>line 27<br>line 28<br>line 29<br>line 30
<br>line 31<br>line 32<br>line 33<br>line 34<br>line 35<br>line 36<br>line 37<br>line 38<br>line 39<br>line 40
<br>line 41<br>line 42<br>line 43<br>line 44<br>line 45<br>line 46<br>line 47<br>line 48<br>line 49<br>line 50
<br>line 51<br>line 52<br>line 53<br>line 54<br>line 55<br>line 56<br>line 57<br>line 58<br>line 59<br>line 60
<br>line 61<br>line 62<br>line 63<br>line 64<br>line 65<br>line 66<br>line 67<br>line 68<br>line 69<br>line 70
</td></tr></table>

<script>document.body.style.overflowY = 'hidden';</script>
</body>

</html>


The scrollbar will reappear after the 'close' button is pressed.

Enjoy!

1 comment:

  1. You can grey out the scrollbar by running this script:

    document.body.style.overflowY = 'scroll';
    document.body.style.position = 'fixed';

    instead of hiding the scrollbar in order to avoid sudden jerky shift of page to the right to fill out the missing scrollbar..

    ReplyDelete