JS简单实用的浮动窗口

<!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>无标题文档</title>
<style type="text/css">
*{ margin:0; padding:0;}
a img{ border:0;}	
</style>
</head>
<body>


<div style="height:2000px; background:#ccc; display:none;">

</div>

<!--div id="floatAd" style=" position:fixed !important; position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight)); z-index: 2147483647; left: 50%; margin-left: -240px !important; top: 50%; margin-top: -210px !important; background:url(images/onlineSay.jpg) no-repeat; width:487px; height:350px; cursor:pointer;">
<div οnclick="closeFAd();" style=" width:26px; height:26px; float:right;"></div>
<div style="width:487px; height:320px; float:right;" οnclick="open_online();"></div>
</div>
<div id="floatSide" style=" width:141px; height:579px; position:fixed !important; position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight)); z-index: 99999; right:0; top:6%; background:url(images/onlineTel.gif);cursor:pointer;" οnclick="open_online();></div-->


</body>
</html>
<script type="text/javascript" src="online.js"></script>

 

 

// JavaScript Document


//浮动广告图片
var floatAdImg = "http://www.onestopweb.cn/online/onlineSay.jpg";
//浮动侧栏图片
var floatSideImg = "http://www.onestopweb.cn/online/onlineTel.gif";

//打开在线沟通
function open_online()
{
	window.open('http://p.qiao.baidu.com//im/index?siteid=7342332&ucid=7601325');
}

//浮动广告
document.writeln("<div id=\"floatAd\" style=\" position:fixed !important; position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight)); z-index: 2147483647; left: 50%; margin-left: -240px !important; top: 50%; margin-top: -210px !important; background:url("+floatAdImg+") no-repeat; width:487px; height:350px; cursor:pointer; display:none; \">");
document.writeln("<div οnclick=\"closeFAd();\" style=\" width:26px; height:26px; float:right;\"></div>");
document.writeln("<div style=\"width:487px; height:320px; float:right;\" οnclick=\"open_online();\"></div>");
document.writeln("</div>");

//浮动侧栏
document.writeln("<div style=\" width:141px; height:579px; position:fixed !important; position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight)); z-index: 99999; right:0; top:6%; background:url("+floatSideImg+");cursor:pointer;\" οnclick=\"open_online();\"></div>");

//关闭浮动广告
function closeFAd()
{
	document.getElementById('floatAd').style.display = 'none';
}

//打开浮动广告
function showFAd()
{
	document.getElementById('floatAd').style.display = 'block';
}

//打开浮动窗口
function showFloat()
{
	document.getElementById('floatAd').style.display = 'block';
}

//打开窗口 20 秒仅执行一次
setTimeout(showFAd,20000);

//每个 30 秒执行一次
setInterval(showFloat,30000);

 

效果图:

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例,使用 JavaScript 和 CSS 实现一个浮动广告: HTML 代码: ``` <div class="floating-ad"> <a href="#"><img src="path/to/image.jpg" alt="Floating Ad"></a> <button class="close-btn">×</button> </div> ``` CSS 代码: ```css .floating-ad { position: fixed; bottom: 20px; right: 20px; width: 300px; height: 250px; border: 1px solid #ccc; background-color: #fff; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3); z-index: 9999; } .close-btn { position: absolute; top: -10px; right: -10px; width: 20px; height: 20px; border: none; background-color: #ccc; color: #fff; font-size: 16px; line-height: 20px; text-align: center; cursor: pointer; } ``` JavaScript 代码: ```js // 获取浮动广告元素和关闭按钮元素 const floatingAd = document.querySelector('.floating-ad'); const closeBtn = document.querySelector('.close-btn'); // 关闭按钮点击事件处理程序 closeBtn.addEventListener('click', () => { floatingAd.style.display = 'none'; }); // 页面滚动事件处理程序 window.addEventListener('scroll', () => { const scrollY = window.scrollY || window.pageYOffset; const adBottom = floatingAd.offsetTop + floatingAd.offsetHeight; const isAdVisible = adBottom > scrollY && floatingAd.offsetTop < scrollY + window.innerHeight; if (isAdVisible) { floatingAd.style.visibility = 'visible'; } else { floatingAd.style.visibility = 'hidden'; } }); ``` 说明: - `position: fixed` 属性使广告固定在浏览器窗口中。`bottom` 和 `right` 属性设置广告距离窗口底部和右侧的距离。 - 广告元素包含一个图片链接和一个关闭按钮,按钮使用绝对定位使其在广告元素的右上角。 - 当点击关闭按钮时,广告元素的 `display` 属性设置为 `none`,使其消失。 - 当页面滚动时,使用 `window.scrollY` 或 `window.pageYOffset` 获取当前滚动位置。如果广告元素的底部在可见窗口内,则将其 `visibility` 属性设置为 `visible`,否则设置为 `hidden`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值