Jquery实现网页右下角弹窗

用Jquery的$.fn.extend()做了小弹窗插件,可惜还不能很好在IE6上工作。。。下面是demo的几个文件,IE6上的工作就交给ie6_tipWindow.js了,有些兼容性问题没弄好,哎,有附源文件~


· Jquery做的弹窗插件tipWindow.js

/*
*这是用JQuery编写的弹出窗口插件会自动将使用popUpWin的Html元素设置为弹出窗口,并放置到右下角
*调用popWindow函数将会使弹出窗口弹出
*popUpWin插件可以用popDown来选择隐藏的动画样式,目前只支持none(直接隐藏)和slip(滑动隐藏)两种样式
*/

$.fn.extend({
    ie6fixedbug: function ie6fixedbug () {
		var args = arguments;
        $(this).css("position", "absolute");
		
        var m = (window.screen.height - $(this).height()) / 2;

        var obj = $(this)[0];
		obj.style.top = (document.body.scrollTop || document.documentElement.scrollTop) + args[0] + 'px';
        window.onscroll = function () {

            obj.style.top = (document.body.scrollTop || document.documentElement.scrollTop) + args[0] + 'px';

        }

        window.onresize = function () {

            obj.style.top = (document.body.scrollTop || document.documentElement.scrollTop) + args[0] + 'px';

        }

    }
}); 

$.fn.extend({
	popUpWin : function() {
	var args = arguments; //保存传入的参数列表
	var $this = this;
		
	if($.browser.msie&&($.browser.version == "6.0")&&!$.support.style) {
		$this.css({position : "absolute"});
	}	
	else {
		$this.css({position : "fixed"});
	}
	$this.css({display : "none", right : "5px"}); //设置弹窗的样式将对象置到右下角
	var $showBottom = 0;  //设置对象显示时的bottom值
	//保存对象的实际高度
	var $winHeight = $this.outerHeight();
	var $hiddenBottom = -$winHeight;  //设置对象隐藏时的bottom值
	if($.browser.msie&&($.browser.version == "6.0")&&!$.support.style) {
		$(this).ie6fixedbug(10);
	} else {
		$this.css("bottom" , $hiddenBottom + "px");  //将对象隐藏到视口以外
	}
	//将对象第一个连接子元素设置为隐藏按钮并绑定click事件
	var $elem = this.find("a:first");
	$elem.click(function() {
		if(args[0]["popdown"] == "none")  //当popdown设置为none时
			$this.css("display" , "none");
		else if(args[0]["popdown"] == "slip") {  //当popdown设置为slip时
			$this.animate({bottom : ($hiddenBottom + "px")} , 450);
		}
	});	
	
},
	
	//调用popWindow使弹窗弹出
	popWindow : function() {
		var $this = this;
		var $id =  $this.attr("id") ? "#" + $this.attr("id") : "." + $this.attr("class");
		var $ii = $this.attr("id");
		$this.css("display" , "block");
		if($.browser.msie)
			$this.animate({bottom : -1}, 450);
		else 
			$this.animate({bottom : 0}, 450);
	}
});

   

IE6上就靠它了:ie6_tipWindow.js

$.fn.extend({
	popUpWinIE : function() {
		var obj = $(this)[0];
window.onload = getMsg;
window.onresize = resizeDiv;
window.onerror = function(){}
//短信提示使用(asilas添加)
var divTop,divLeft,divWidth,divHeight,docHeight,docWidth,objTimer,i = 0;
function getMsg()
{
  try{
  divTop = parseInt(obj.style.top,10)
  divLeft = parseInt(obj.style.left,10)
  divHeight = parseInt(obj.offsetHeight,10)
  divWidth = parseInt(obj.offsetWidth,10)
  docWidth = document.body.clientWidth;
  docHeight = document.body.clientHeight;
  obj.style.top = parseInt(document.body.scrollTop,10) + docHeight + 10;// divHeight
  obj.style.left = parseInt(document.body.scrollLeft,10) + docWidth - divWidth
  obj.style.visibility="visible"
  objTimer = window.setInterval("moveDiv()",10)
  }
  catch(e){}
}

function resizeDiv()
{
  i+=1
  if(i>500) closeDiv()
  try{
  divHeight = parseInt(obj.offsetHeight,10)
  divWidth = parseInt(obj.offsetWidth,10)
  docWidth = document.body.clientWidth;
  docHeight = document.body.clientHeight;
  obj.style.top = docHeight - divHeight + parseInt(document.body.scrollTop,10)
  obj.style.left = docWidth - divWidth + parseInt(document.body.scrollLeft,10)
  }
  catch(e){}
}

function moveDiv()
{
  try
  {
  if(parseInt(obj.style.top,10) <= (docHeight - divHeight + parseInt(document.body.scrollTop,10)))
  {
  window.clearInterval(objTimer)
  objTimer = window.setInterval("resizeDiv()",1)
  }
  divTop = parseInt(obj.style.top,10)
  obj.style.top = divTop - 1
  }
  catch(e){}
}
function closeDiv()
{
  obj.style.visibility='hidden';
  if(objTimer) window.clearInterval(objTimer)
}
	}
});

网页文件win.html

<!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" />
<link rel="stylesheet" type="text/css" href="reset.css" />  <!--重置浏览器样式-->
<!--[if IE 6]>
<style type="text/css">
* html,* html body{background-image:url(about:blank);background-attachment:fixed}
</style>
<![endif]-->
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="tipWindow.js"></script>
<style type="text/css">
#tipWin {
	background: url(images/bg_pop_window.png) left top no-repeat;
	width: 279px;
	height: 186px;
	position: fixed;
}

#hiddenButton, #hiddenButton:link {
	border: none;
	border-width: 0; 	/*IE 6 BUG*/
	border-style: none; /*IE 6 BUG*/
	display: block;
	margin: -18px 15px 0px 245px;
	padding: 2px 2px 2px 2px;
	background: url(images/hiddenButton.png) left top no-repeat;
}

#hiddenButton img {
	border: none;
	border-width: 0; 	/*IE 6 BUG*/
	border-style: none; /*IE 6 BUG*/
}

.popWinTitle {
	display: block;
	margin: 5px 0 0 5px;
	font: 0.9em "幼圆";
	color: #666;
}
</style>
<script type="text/javascript">
$(function(){
	$('#tipWin').popUpWin({
		"popdown" : "none"
	});
	
	$('#tipWin').popWindow();
	$('#try').ie6fixedbug(100);
});
</script>
<title>无标题文档</title>
</head>

<body>
<div id="try">123</div>
<div style="height: 1000px; width: 100px;"></div>
<div id="tipWin">
<span class="popWinTitle">消息提示</span>
<a id="hiddenButton" href="javascript:;"><img src="images/hiddenButton.png" /></a>
</div>
</body>
</html>

重置样式的CSS:reset.css

html, body, h1, h2, h3, h4, h5, h6, p, ol, ul, li, pre, code, address, variable, form, fieldset, blockquote {
 padding: 0;
 margin: 0;
 font-size: 100%;
 font-weight: normal;
}
table { border-collapse: collapse; border-spacing: 0; }
td, th, caption { font-weight: normal; text-align: left; }
img, fieldset { border: 0px; }
ol { padding-left: 1.4em; list-style: decimal; }
ul { padding-left: 1.4em; list-style:square; }
q:before, q:after { content:''; }
*{padding: 0; margin: 0;};


### 回答1: 要实现类似于美团的右下角弹窗,可以借助jQuery的推送消息功能来完成。具体步骤如下: 首先,需要在页面里引入jQuery的库文件。 其次,编写相应的html、css和js代码,实现弹窗的样式和内容。可以在页面上任意位置添加一个隐藏的div元素,用来存储弹窗的具体内容和样式。比如: ```html <div id="push-message" style="display:none"> <div class="push-message-header"> <span class="push-message-title">新品上线啦!</span> <a class="push-message-close">×</a> </div> <div class="push-message-body"> <img src="new-product.png" alt="新品图片"> <p>快来抢购新品,先到先得!</p> </div> </div> ``` 然后,在js代码中,使用jQuery的animate()方法来实现弹窗动画效果,并调用setTimeout()方法来控制弹窗的显示时间和关闭按钮的点击事件。代码示例如下: ```javascript $(document).ready(function(){ // 弹窗显示动画效果 $("#push-message").animate({bottom:"20px"}, 1000); // 弹窗自动隐藏 setTimeout(function(){ $("#push-message").fadeOut(); }, 5000); // 关闭按钮点击事件 $(".push-message-close").click(function(){ $("#push-message").fadeOut(); }); }); ``` 最后,需要在页面中调用该js文件,使弹窗能够正常显示和关闭。 总体来说,实现类似于美团的右下角弹窗并不难,只需要按照上述步骤进行相应的设置和调整即可。 ### 回答2: 实现类似于美团的右下角弹窗,可以使用jQuery推送消息的方法。 具体实现步骤如下: 1.在页面底部引入jQuery库文件。 2.定义一个DIV标签作为弹窗容器,设置其样式为固定定位,底部距离为0,右侧距离为0,宽度和高度自定义大小。 3.编写jQuery代码,实现弹窗弹出和隐藏: a)当页面加载完成时,设置弹窗容器的初始位置为底部右侧隐藏状态; b)当触发某个事件时(如点击按钮事件),在弹窗容器中添加需要显示的内容,并将容器位置从底部右侧滑动到底部左侧显示状态; c)当点击弹窗容器的关闭按钮时,将容器从底部左侧滑动到底部右侧隐藏状态。 4.可根据需求自定义弹窗的点击事件、弹出/隐藏动画效果和定时器等。 总之,使用jQuery推送消息完成类似于美团的右下角弹窗功能,是一种简单、方便、易用的实现方式。 ### 回答3: 要实现类似于美团右下角弹窗的效果,可以使用jQuery推送消息插件。该插件支持在网页中创建自定义的弹窗,并按照设定的方式进行推送。 首先,在页面中引入jQuery库和jquery推送消息插件,之后可以在JavaScript文件中进行如下操作: 1. 定义推送消息的内容和样式: var options = { 'title': '新消息提醒', 'text': '您有一条新的优惠信息,快来看看吧!', 'sticky': true, 'time': '', 'class_name': 'my-sticky-class' }; 2. 调用插件的弹窗方法: $.sticky(options); 其中,options是一个对象,包含了弹窗的标题(title)、内容(text)、是否固定不消失(sticky)、自动关闭的时间(time)、自定义类名(class_name)等参数。 3. 设定弹窗的样式和位置: .sticky { background-color: #FFFFFF; border: 1px solid #DDDDDD; box-shadow: 0 1px 6px rgba(0, 0, 0, 0.1); color: #444444; font-family: 'Arial', sans-serif; font-size: 14px; line-height: 1.5em; margin-top: 10px; padding: 10px 15px; position: absolute; right: 0; bottom: 0; width: 300px; } 通过调整样式和位置,可以使弹窗呈现出美观且醒目的效果。 总之,使用jQuery推送消息插件,可以轻松实现类似于美团右下角弹窗的效果,且可根据需求进行自定义设置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值