css实现锚点定位的几种方法

8 篇文章 0 订阅
2 篇文章 0 订阅

css锚点定位的一些方法

方法一

1.效果图

在这里插入图片描述

在这里插入图片描述

2.实现代码
html代码
import React from 'react';
import './index.css';

const Anchor = () => {
    return (
        <div>
            <div className="top">
                <label htmlFor="c1">part1</label>
                <label htmlFor="c2">part2</label>
                <label htmlFor="c3">part3</label>
            </div>
            <input type="radio" name="page" id="c1" checked />
            <div id="div-1" className="page">
                <h1>第一部分</h1>
            </div>
            <input type="radio" name="page" id="c2" />
            <div id="div-2" className="page">
                <h1>第二部分</h1>
            </div>
            <input type="radio" name="page" id="c3" />
            <div id="div-3" className="page">
                <h1>第三部分</h1>
            </div>
        </div>
    );
}
export default Anchor;
css代码
* {
    margin: 0;
    padding: 0
}

html,
body {
    height: 100%;
    height: 100%;
    background: #a3ceff;
    overflow: hidden;
}

.top {
    width: 100%;
    height: 40px;
    line-height: 40px;
    background: rgba(63, 202, 13, 0.6);
    position: fixed;
    z-index: 9;
}

.top label {
    display: inline-block;
    width: 30%;
    color: #fff;
    text-align: center;
    cursor: pointer;
}

.top label:hover {
    background: rgba(0, 0, 0, .4);
}

input {
    display: none;
}

h1 {
    color: #fff;
    padding-top: 20%;
}

.page {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    min-height: 100%;
    background: #3C98FF;
    text-align: center;
    transform: translateY(-100%);
    transition: .6s;
}


/**
* 作用是为了控制过渡动画 执行的时间
* 具体效果可依据实际需求来确定
*/
input[type=radio]:checked+.page {
    transform: translateY(0);
    animation: fromBtm 0.5s;
}

@keyframes fromBtm {

    /**
    * 过渡动画效果在Y轴距离上的占比
    */
    from {
        transform: translateY(100%);
    }

    to {}
}

方法二

1.效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

html代码
import React from 'react';
import './AnchorExample.css'

const AnchorExample = () => {
    return (
        <div>
            <section>
                <h1>
                    {/**可以使用id也可以使用name的方法,本例使用name */}
                    {/**如果有面包屑遮挡可视区域,可以通过相对定位让内容向下移出被面包屑遮挡部分 */}
                    <a name='c1' style={{ position: 'relative', top: '-100px' }} />
                    <div>第一部分</div>
                </h1>
            </section>
            <section>
                <h1>
                    <a name='c2' />
                    <div>第二部分</div>
                </h1>
            </section>
            <section>
                <h1>
                    <a name='c3' />
                    <div>第三部分</div>
                </h1>
            </section>
            <section>
                <h1>
                    <a name='c4' />

                    <div>第四部分</div>
                </h1>
            </section>
            <section>
                <h1>
                    <a name='c5' />

                    <div>第五部分</div>
                </h1>
            </section>
            <section>
                <h1>
                    <a name='c6' />
                    <div>第六部分</div>
                </h1>
            </section>
            {
                <nav>
                    <ul>
                        <li><a href="#c1">第一部分</a></li>
                        <li><a href="#c2">第二部分</a></li>
                        <li><a href="#c3">第三部分</a></li>
                        <li><a href="#c4">第四部分</a></li>
                        <li><a href="#c5">第五部分</a></li>
                        <li><a href="#c6">第六部分</a></li>
                    </ul>
                </nav>
            }
        </div>
    );
}
export default AnchorExample;

css代码
* {
    margin: 0;
    padding: 0
}

html,
body {
    /* height: 100%; */
    /* height: 100%; */
    background: #a3ceff;
    overflow: auto;
}


/**
* 鼠标没有划上去的效果  
* 通过定位可实现锚点 浮框定位到可视页面的中间
*/
nav {
    position: fixed;
    top: 50%;
    right: 0;

    width: 40px;
    height: 40px;
    background-color: rgba(250, 179, 80, 0.8);
    border: 5px solid rgba(204, 204, 204, 0.5);
    border-radius: 50%;

    font-size: 0;
    overflow: hidden;
    cursor: pointer;
}

/**
* 鼠标划上去后的效果
*/
nav:hover {
    position: fixed;
    top: 50%;
    right: 0;

    width: 120px;
    height: 150px;
    background-color: rgba(250, 179, 80, 0.8);
    border: 1px solid rgba(204, 204, 204, 0.5);
    border-radius: 10px;
    font-size: 14px;
}

nav li {
    margin: 5px 10px;

}

li a {
    color: #fff;
}

li a:hover {
    color: #bb29d5;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值