Thứ Ba, 14 tháng 2, 2012

Hướng dẫn tạo popup khi vào website

Để tạo Popup khi vào website bạn xem ví dụ sau

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Làm 1  cái popup đơn giản nào</title>
<!-- 
Dẫn thư viện jquery từ host của google-->
<
script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {    

        var id = '#dialog';
    
        //Lấy chiều cao và rộng của màn hình nè
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
    
        //Rùi định dạng chiều cao và rộng cho cái vùng div chứa cái popup
        $('#mask').css({'width':maskWidth,'height':maskHeight});
        
        //hiệu ứng xuất hiện    
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow",0.8);
    
              
        //làm cho cái popup canh giữa màn hình
        $(id).css('top',  maskHeight/2-$(id).height()/2);
        $(id).css('left', maskWidth/2-$(id).width()/2);
    
        //hiệu ứng cho cái popup xuất hiện (xuất hiện từ từ trong vòng 2s)
        $(id).fadeIn(2000);     
    
    //Nếu người ta click vào nút close nè
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        
        $('#mask').hide();
        $('.window').hide();
    });        
    
    //Nếu vùng div id mask được click thì ẩn nó đi nè
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });        
    
});

</script>
<!-- nhúng 1 ít  css nhé-->
<style type="text/css">
body {
font-family:verdana;
font-size:15px;
}

a {color:#333; text-decoration:none}
a:hover {color:#ccc; text-decoration:none}

#mask {
  position:absolute;
  left:0;
  top:0;
  z-index:9000;
  background-color:#000;
  display:none;
}  
#boxes .window {
  position:absolute;
  left:0;
  top:0;
  width:440px;
  height:200px;
  display:none;
  z-index:9999;
  padding:20px;
}
#boxes #dialog {
  width:375px; 
  height:203px;
  padding:10px;
  background-color:#ffffff;
}
</style>
</head><body>
<h2><a href="http://www.qhonline.info/">Tự làm 1 cái popup đơn giản nè</a></h2>

<div id="boxes">
<div style="top: 199.5px; left: 551.5px; display: none;" id="dialog" class="window">
Ta là cái popup đây :))
<a href="#" class="close">Đóng lại</a>
</div>
<!-- vùng div id mask nè. lúc đầu nó sẽ ẩn -->
<div style="width: 1478px; height: 602px; display: none; opacity: 0.8;" id="mask"></div>
</div>
</body>
</html>  

Cái này gọi popup on page .Các bạn có thể tuy chỉnh cookie để người dùng vào chỉ hiện lên 1 lần hoặc sử dụng Javascript
http://www.dynamicdrive.com/dynamicindex11/dhtmlad.htm

Không có nhận xét nào:

Đăng nhận xét