jQuery实现中间广告位效果

学习资料

http://v.apelearn.com/student.php

要求

展现广告的位置固定在页面的右侧中间位置;

知识点

1.IE6不兼容position:fixed;的解决方案

position: fixed;

_position: absolute;/*IE6不兼容fixed*/

2. 解决抖动的方案

body {

/*解决抖动:背景固定*/

background-image: url();

background-attachment: fixed;

}

3.屏幕可视区域的获取

//javascript

document.documentElement.clientHeight

document.documentElement.clientWidth

//jQuery

$(window).height();

S(window).width();

4.元素实际尺寸的获取

//JavaScript

obj.offsetHeight

obj.offsetWidth

//jQuery

outerHeight()

outerWidth()

5.浏览器版本检测

window.navigator.userAgent.toLowerCase().indexOf('IE 6.0')

6.滚动高度的获取

document.documentElement.clientHeight || document.body.scrollTop;

7.滚动条滚动事件

scroll

8.其他

获取览器显示区域的高度 : $(window).height();

获取浏览器显示区域的宽度 :$(window).width();

获取页面的文档高度 :$(document).height();

获取页面的文档宽度 :$(document).width();

获取滚动条到顶部的垂直高度 :$(document).scrollTop();

获取滚动条到左边的垂直宽度 :$(document).scrollLeft();

实现思路

1.获取广告位容器的实际高度;

2.获取屏幕可视区的高度;

3.计算居中的位置;

4.兼容性解决;

Demo代码

HTML

<div class="box"></div>

CSS

body{height:3000px;}

.box{

/*解决抖动的问题*/

position:fixed;_position:absolute;/*IE6不兼容fixed*/right:0;top:0;width:200px;height:200px;line-height:200px;background-color:orangered;color:#fff;text-align:center;}

body{

/*解决抖动:背景固定*/

background-image:url();background-attachment:fixed;}

JS

$(function() {

/* 盒子的实际尺寸:本身高度+上下边框+上下的内边距 */

variBoxH =$('.box').outerHeight();

/* 获取可视区的高度 */

variClientHeight =$(window).height();

/* 居中定位: (屏幕可视区域的高度 - 盒子本身的高度)/2

* 即是:(可视区域 - 元素尺寸)/2*/

variPosition = (iClientHeight - iBoxH)/2;

$('.box').css('top',iPosition);

$(window).scroll(function() {

if(window.navigator.userAgent.toLowerCase().indexOf('IE 6.0') !== -1) {

variScrollTop =$(document).scrollTop();

alert(iScrollTop);

$('.box').css('top',(iPosition + iScrollTop));

}

});

});

效果


中间广告位效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,796评论 1 92
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 1,891评论 0 1
  • Window和document对象的区别 window对象window对象表示浏览器中打开的窗口window对象是...
    FConfidence阅读 2,244评论 0 5
  • 欢迎来找我玩啊
    唐宇威威阅读 227评论 5 6
  • 在英语启蒙方面我走了太多太多的弯路。最主要的原因就是我的英语太差,从最开始的汪培鋌书单到后来的没有目的磨耳朵还有...
    大鱼海棠brave阅读 611评论 0 0