Jq实现浮动飘窗广告位

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>jquery漂浮广告代码</title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
    <style>
    </style>
</head>

<body>
    <div id="imgDiv" style="position:absolute;left:50px;top:60px;">
        <div id="a" style="width:20px;height:20px;position:absolute;left:160px;background:salmon;text-align: center;">×
        </div>
        <!-- <img src="01.jpg" border="0" /> -->
        <div style="width:180px;height:180px;background:red;"></div>
    </div>

    <script>
        var xin = true, // true向右偏移,false向左偏移,首次为true,默认向右偏移
            yin = true; // true向下偏移,false向上偏移,首次为true,默认向下偏移
        var step = 1; // 每次移动偏移量
        var delay = 10; // 定时函数定时时间
        var $obj;
        $(function () {
            $obj = $("#imgDiv");
            var time = window.setInterval("move()", delay);
            $obj.mouseover(function () {
                clearInterval(time)
            });
            $obj.mouseout(function () {
                time = window.setInterval("move()", delay)
            });
        });

        function move() {
            var left = $obj.offset().left;
            var top = $obj.offset().top;
            var L = T = 0; //左边界和顶部边界
            var R = $(window).width() - $obj.width(); // 右边界
            var B = $(window).height() - $obj.height(); // 下边界

            //难点:怎样判断广告的4个边框有没有超出可视化范围!
            if (left < L) {
                xin = true; // 水平向右移动
            }
            if (left > R) {
                xin = false; // 水平向左移动
            }
            if (top < T) {
                yin = true; // 垂直向下移动
            }
            if (top > B) {
                yin = false; // 垂直向上移动
            }
            //根据有没有超出范围来确定广告的移动方向
            left += step * (xin == true ? 1 : -1);
            top += step * (yin == true ? 1 : -1);
            // 给div 元素重新定位
            $obj.offset({
                top: top,
                left: left
            })
        }
        //关闭
        $(function () {
            $("#a").click(function () {
                var b = $("#a").parent();
                $(b).remove();
            })
        })
    </script>

</body>

</html>

 

HTML浮动广告飘窗是一种常见的网页广告形式,通常在网页的某个固定位置浮动或弹出,以吸引用户的注意力。以下是一些实现HTML浮动广告飘窗的基本步骤: ### 1. HTML结构 首先,创建一个HTML结构来包含广告内容: ```html <div id="floating-ad" style="position: fixed; bottom: 10px; right: 10px; width: 300px; height: 250px; background-color: #f0f0f0; border: 1px solid #ccc; display: none;"> <span id="close-ad" style="position: absolute; top: 5px; right: 5px; cursor: pointer; font-weight: bold;">X</span> <p>这里是广告内容</p> </div> ``` ### 2. CSS样式 通过CSS样式来美化广告飘窗: ```css #floating-ad { position: fixed; bottom: 10px; right: 10px; width: 300px; height: 250px; background-color: #f0f0f0; border: 1px solid #ccc; display: none; z-index: 1000; } #close-ad { position: absolute; top: 5px; right: 5px; cursor: pointer; font-weight: bold; } ``` ### 3. JavaScript交互 使用JavaScript来控制广告飘窗的显示和隐藏: ```javascript window.onload = function() { var ad = document.getElementById('floating-ad'); var closeBtn = document.getElementById('close-ad'); // 显示广告飘窗 ad.style.display = 'block'; // 关闭广告飘窗 closeBtn.onclick = function() { ad.style.display = 'none'; }; }; ``` ### 4. 完整示例 将上述代码整合在一起,形成一个完整的示例: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>浮动广告飘窗示例</title> <style> #floating-ad { position: fixed; bottom: 10px; right: 10px; width: 300px; height: 250px; background-color: #f0f0f0; border: 1px solid #ccc; display: none; z-index: 1000; } #close-ad { position: absolute; top: 5px; right: 5px; cursor: pointer; font-weight: bold; } </style> </head> <body> <div id="floating-ad"> <span id="close-ad">X</span> <p>这里是广告内容</p> </div> <script> window.onload = function() { var ad = document.getElementById('floating-ad'); var closeBtn = document.getElementById('close-ad'); // 显示广告飘窗 ad.style.display = 'block'; // 关闭广告飘窗 closeBtn.onclick = function() { ad.style.display = 'none'; }; }; </script> </body> </html> ``` 通过上述步骤,你可以创建一个简单的HTML浮动广告飘窗,并根据需要进行进一步的样式和功能扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值