使用DIV彈出窗口來動態(tài)顯示內(nèi)容的原理:首先采用CSS和HTML隱藏彈窗中的內(nèi)容,然后利用JavaScript(本教程中是JQuery)來動態(tài)顯示它們。這種效果不僅能夠充分利用有限的版面空間,而且能夠提高用戶體驗;更重要的是,它并不影響SEO效果(因為它實際存在于頁面中,只是初始為不可見狀態(tài))
1、在html頁面中定義一個div,并在div實現(xiàn)我們需要展示的內(nèi)容。
復(fù)制代碼 代碼如下:
<body>
<div>
<h2><img src="images/close.png" />網(wǎng)站登錄</h2>
<form >
<div></div>
<div>帳 號:<input type="text" /></div>
<div>密 碼:<input type="password" /></div>
<div><input type="button" value="" /></div>
</form>
<div>注冊新用戶 | 忘記密碼?</div>
</div>
</body>
復(fù)制代碼 代碼如下:
#login {
width:350px;
height:250px;
border:1px solid #ccc;
position:absolute;
display:block;
z-index:9999;
background:#fff;
}
#login h2 {
height:40px;
line-height:40px;
text-align:center;
font-size:14px;
letter-spacing:1px;
color:#666;
background:url(images/login_header.png) repeat-x;
margin:0;
padding:0;
border-bottom:1px solid #ccc;
cursor:move;
}
#login h2 img {
float:right;
position:relative;
top:14px;
right:8px;
cursor:pointer;
}
#login div.info {
padding:10px 0 5px 0;
text-align:center;
color:maroon;
}
#login div.user, #login div.pass {
font-size:14px;
color:#666;
padding:5px 0;
text-align:center;
}
#login input.text {
width:200px;
height:25px;
border:1px solid #ccc;
background:#fff;
font-size:14px;
}
#login .button {
text-align:center;
padding:15px 0;
}
#login input.submit {
width:107px;
height:30px;
background:url(images/login_button.png) no-repeat;
border:none;
cursor:pointer;
}
#login .other {
text-align:right;
padding:15px 10px;
color:#666;
}
復(fù)制代碼 代碼如下:
$(document).ready(function()
{
jQuery.fn.extend({
center:function(width,height)
{
return $(this).css("left", ($(window).width()-width)/2+$(window).scrollLeft()).
css("top", ($(window).height()-height)/2+$(window).scrollTop()).
css("width",width).
css("height",height);
}
});
});
復(fù)制代碼 代碼如下:
$(".login").click(function ()
{
$("#login").show().center(350,250);//展現(xiàn)登陸框
});
復(fù)制代碼 代碼如下:
$(document).ready(function()
{
jQuery.fn.extend({
//拖拽功能
drag:function(){
var $tar = $(this);
return $(this).mousedown(function(e){
if(e.target.tagName =="H2"){
var diffX = e.clientX - $tar.offset().left;
var diffY = e.clientY - $tar.offset().top;
$(document).mousemove(function(e){
var left = e.clientX - diffX;
var top = e.clientY - diffY;
if (left < 0){
left = 0;
}
else if (left <= $(window).scrollLeft()){
left = $(window).scrollLeft();
}
else if (left > $(window).width() +$(window).scrollLeft() - $tar.width()){
left = $(window).width() +$(window).scrollLeft() -$tar.width();
}
if (top < 0){
top = 0;
}
else if (top <= $(window).scrollTop()){
top = $(window).scrollTop();
}
else if (top > $(window).height() +$(window).scrollTop() - $tar.height()){
top = $(window).height() +$(window).scrollTop() - $tar.height();
}
$tar.css("left",left + 'px').css("top",top + 'px');
});
}
$(document).mouseup(function(){
$(this).unbind("mousemove");
$(this).unbind("mouseup")
});
});
}
});
});
復(fù)制代碼 代碼如下:
$("#login").drag();
新聞熱點
疑難解答
圖片精選