html5end锚点,页面如何做锚点?

最近项目中有一个需求,弹窗的表格列表需要自动定位到当前已选的行,实现之后顺带归总以下锚点有哪些实现方法。

1、window.location.hash

1.1、#的涵义

很多同学应该不了解hash,下面简单说说hash的作用:

#代表网页中的一个位置。其右面的字符,就是该位置的标识符。比如,http://www.example.com/index.html#print

就代表网页index.html的print位置。浏览器读取这个URL后,会自动将print位置滚动至可视区域。

为网页位置指定标识符,有两个方法。一是使用锚点,比如,二是使用id属性,比如

1.2、HTTP请求不包括#

#是用来指导浏览器动作的,对服务器端完全无用。所以,HTTP请求中不包括#。

比如,访问下面的网址,http://www.example.com/index.html#print

浏览器实际发出的请求是这样的:GET **/index.html** HTTP/1.1

Host: www.example.com

可以看到,只是请求index.html,根本没有"#print"的部分。

1.3、#后的字符

在第一个#后面出现的任何字符,都会被浏览器解读为位置标识符。这意味着,这些字符都不会被发送到服务器端。

比如,下面URL的原意是指定一个颜色值:http://www.example.com/?color=#fff

但是,浏览器实际发出的请求是:GET **/?color=** HTTP/1.1

Host: www.example.com

可以看到,"#fff"被省略了。只有将#转码为%23,浏览器才会将其作为实义字符处理。也就是说,上面的网址应该被写成:http://example.com/?color=%23fff

1.4、改变#不触发网页重载

单单改变#后的部分,浏览器只会滚动到相应位置,不会重新加载网页。

比如,从http://www.example.com/index.html#location1

改成http://www.example.com/index.html#location2

浏览器不会重新向服务器请求index.html。

1.5、改变#会改变浏览器的访问历史

每一次改变#后的部分,都会在浏览器的访问历史中增加一个记录,使用"后退"按钮,就可以回到上一个位置。

这对于ajax应用程序特别有用,可以用不同的#值,表示不同的访问状态,然后向用户给出可以访问某个状态的链接。

值得注意的是,上述规则对IE 6和IE 7不成立,它们不会因为#的改变而增加历史记录。

1.6、window.location.hash读取#值

window.location.hash这个属性可读可写。读取时,可以用来判断网页状态是否改变;写入时,则会在不重载网页的前提下,创造一条访问历史记录。

1.7、onhashchange事件

这是一个HTML 5新增的事件,当#值发生变化时,就会触发这个事件。IE8+、Firefox 3.6+、Chrome 5+、Safari 4.0+支持该事件。

它的使用方法有三种:window.onhashchange = func;

window.addEventListener("hashchange", func, false);

对于不支持onhashchange的浏览器,可以用setInterval监控location.hash的变化。

1.8、页面如何使用hash锚点呢?

弹出弹窗时,表格列表自动定位到id=011的那一行。实现方式很简单,只要在id=011的那一行数据的元素中加入一个id,然后在useEffect页面初始化时window.location.hash = 刚才的设置的id,具体实现如下:import React, { useEffect, useState } from 'react';

import { RouteConfigComponentProps } from 'react-router-config';

import MainStore from '../../store/MainStore';

import { inject, observer } from 'mobx-react';

import { Modal, Table } from 'antd';

import data from '../data';

import styles from './index.module.scss';

interface IProps extends RouteConfigComponentProps {

mainStore?: MainStore;

}

const TableCom = (props: IProps) => {

const { mainStore } = props;

const { visible, setVisible } = mainStore as MainStore;

useEffect(() => {

window.location.hash = 'anchor';

})

const columns = [

{

title: 'ID',

dataIndex: 'id',

key: 'id',

width: 80,

render: (text: any) => {

return

}

},

{

title: '姓名',

dataIndex: 'name',

key: 'name',

width: 80,

},

{

title: '年龄',

dataIndex: 'age',

key: 'age',

width: 80,

},

{

title: '学校',

dataIndex: 'school',

key: 'school',

width: 150

},

{

title: '身高',

dataIndex: 'height',

key: 'height',

width: 80,

},

]

return (

title="列表"

visible={visible}

onOk={() => setVisible(false)}

onCancel={() => setVisible(false)}

width={600}

>

columns={columns}

dataSource={data}

pagination={false}

scroll={{y: 200}}

rowClassName={(record, index) => record.id === '011' ? `${styles.bgColor}` : ''}

>

)

}

export default inject('mainStore')(observer(TableCom));

效果如下:

bVcK0X3

2、scrollIntoView

html5新增api,可以定位到滚动容器可视区域或者滚动区域正中间,可以设置滚动滑动效果,但存在浏览器兼容性。具体用法传送门:https://developer.mozilla.org...

具体实现如下:import React, { useEffect, useState } from 'react';

import { RouteConfigComponentProps } from 'react-router-config';

import MainStore from '../../store/MainStore';

import { inject, observer } from 'mobx-react';

import { Modal, Table } from 'antd';

import data from '../data';

import styles from './index.module.scss';

interface IProps extends RouteConfigComponentProps {

mainStore?: MainStore;

}

const TableCom = (props: IProps) => {

const { mainStore } = props;

const { visible, setVisible } = mainStore as MainStore;

const [anchor, setAnchor] = useState(null);

useEffect(() => {

if (anchor) {

anchor.scrollIntoView({block: 'end'});

}

// 或者

// const anchor = document.getElementById('anchor');

// anchor?.scrollIntoView({block: 'end'})

})

const columns = [

{

title: 'ID',

dataIndex: 'id',

key: 'id',

width: 80,

render: (text: any) => {

return

ref={(el) => {

if(text === '011') {

setAnchor(el)

}

}}>text

}

},

{

title: '姓名',

dataIndex: 'name',

key: 'name',

width: 80,

},

{

title: '年龄',

dataIndex: 'age',

key: 'age',

width: 80,

},

{

title: '学校',

dataIndex: 'school',

key: 'school',

width: 150

},

{

title: '身高',

dataIndex: 'height',

key: 'height',

width: 80,

},

]

return (

title="列表"

visible={visible}

onOk={() => setVisible(false)}

onCancel={() => setVisible(false)}

width={600}

>

columns={columns}

dataSource={data}

pagination={false}

scroll={{y: 200}}

rowClassName={(record, index) => record.id === '011' ? `${styles.bgColor}` : ''}

>

)

}

export default inject('mainStore')(observer(TableCom));

效果:

bVcK0Ye

可以看到scrollIntoView锚点的行为更加准确。

3、scrollTop、offsetTop

操作dom进行锚点是一种最常见方式,这里不进行描述。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值