锚点定位到顶部html,锚点定位如何不定位到最顶端?

本文介绍了一种使用CSS解决锚点定位时,内容被顶部元素遮挡的问题。通过设置适当的边距和背景色,确保链接能够正确指向内容区域。同时,文章还展示了如何使用JavaScript实现滚动时自动高亮导航菜单项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网页定位导航效果扩展案例

* {

margin: 0;

padding: 0;

}

body {

font-size: 12px;

line-height: 1.7;

background-color: #888;

}

li {

list-style: none;

}

#content {

width: 1000px;

margin: 0 auto;

padding-top: 0;

padding-bottom: 300px;

}

/*请补充此处代码,让右侧广告正确显示*/

.item{

margin: 0 0 100px 125px;

border-top:100px solid #888; padding-left: 170px;}

#content #item1 {

background: url("http://img1.sycdn.imooc.com/536c9ea800013ae610000540.jpg") no-repeat center top;

}

#content #item2 {

background: url("http://img1.sycdn.imooc.com/536c9ecd0001606d10000540.jpg") no-repeat center top;

}

#content #item3 {

background: url("http://img1.sycdn.imooc.com/536c9f0a0001ccc310000540.jpg") no-repeat center top;

}

#content #item4 {

background: url("http://img1.sycdn.imooc.com/536c9f6700012e9010000540.jpg") no-repeat center top;

}

#content #item5 {

background: url("http://img1.sycdn.imooc.com/536c9f800001b28510000540.jpg") no-repeat center top;

}

/*请补充此处代码,让导航菜单在左侧绝对定位显示*/

#menu{position: fixed;

left: 100px;

top: 100px;

}

#menu h1 {

color: #fff;

font-size: 14px;

text-align: center;

background: #ee1111;

line-height: 30px;

}

#menu ul li a {

display: block;

margin: 5px 0;

font-size: 12px;

color: #ccc;

width: 160px;

padding: 10px 20px;

text-decoration: none;

text-align: left;

border-bottom: 1px dashed #999;

}

#menu ul li a strong {

color: #f1f1f1;

}

#menu ul li a:hover,

#menu ul li a.current {

color: #fff;

background: #666;

}

/*ie6 hack*/

* html, * html body {

background-image: url(about:blank);

background-attachment: fixed;

}

* html #menu {

/*position: fixed;*/

position: absolute;

top: expression(((e=document.documentElement.scrollTop)?e:document.body.scrollTop)+100+'px');

}

$(document).ready(function () {

$(window).scroll(function () {

var top = $(document).scrollTop();

var menu = $("#menu");

var items = $("#content").find(".item");

var currentId="";

// 请补充此处代码,让导航菜单实现在滚动条滚动的时候自动设置焦点

items.each(function(){

if(top>$(this).offset().top-330){

currentId="#"+$(this).attr("id");

}else{return false;}

});

var currentLink= menu.find(".current");

if(currentId&&currentLink!=currentId) {

currentLink.removeClass("current");

menu.find("[href=" + currentId + "]").addClass("current");

}

});

});

6a3e11fab29468f47325c7b991f2b0cb.png

ae43790bf02c166c9551ebadffaa3019.png

5dda1b5d74d12a8e3d5bfe4c0894dc7f.png

ebdf0d67cba504975fe88f0f0bfcffe3.png

7359e3aa467b6c5d15b67ea7f6bccc1d.png

我的解决办法是将border-top扩大,颜色设置为背景色  ,想问下有没有更好的办法?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值