博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
On :target
阅读量:7119 次
发布时间:2019-06-28

本文共 5687 字,大约阅读时间需要 18 分钟。

The :target pseudo selector in CSS matches when the hash in the URL and the id of an element are the same.

The current hash of the URL is "voters"
Content
:target {
background: yellow; }

While that URL is as it is, that section element will have a yellow background, as per our CSS.

When would you use this?

One possibility is when you want style with "states." When the page has a certain hash, it's in that state. It's not quite as versatile as manipulating class names (since there can only be one and it can only be related to one element) but it's similar. Anything you could do changing a class to change state you could do when the element is in :target. For instance: change colors, change position, change images, hide/show things, whatever.

I'd use these rules-of-thumb for when :target is a good choice:

  1. When a "state" is needed
  2. When the jump-down behavior is acceptable
  3. When it's acceptable to affect the browser history

We'll touch on all these things in this article.

How do you get hashes in URLs?

The most common way is by a user clicking a link which includes a hash. Could be an internal (same-page) link or a fully qualified URL that happens to end with a hash and value. Examples:

Go To There  Go To There

Jumping Behavior

Regardless if it's a same-page link or not, the browser behavior is the scroll the page until that element is at the top of the page. Or, as far as it can if it can't scroll that far. This is rather important to know, because it means exploiting this "stated" behavior is a bit tricky/limited.

For instance, I once to replicate functional CSS tabs, but ultimately decided using the was a because it avoids the page-jumping issues. Ian Hansson at CSS Science has some as well. His third example uses :target, and absolutely positioned elements hidden above the top of the page to prevent page jumping behavior. It's clever, but not really a solution, because that would mean the page would jump upwards should the tabs be down further on a page.

A perfect use: highlighting sections

Here's a problem: When a hash-link sends you flying down the page to the relevant section, it will try and make that section snug against the top of the browser window.

But what if there isn't enough room to scroll beneath that section? That section will be visible, but it won't be snug against the top, which can be weird and confusing.

It can be disorienting.

I'm not just making that up. From personal experience, page-jumping links that don't take me to somewhere where was I was linking to is exactly on the top, I get all out of sorts. I find it happens all to often on things like FAQ pages where the linked-to sections often aren't very tall.

So let's solve that!

One historical method was called the . It was employed by 37 signals in situations where new content is added to the page, and they were trying to draw the user's attention to it. Jonathan Snook and combined it with :target.

Instead of yellow fade, we'll indicate which section the link we just clicked was referring to by nudging it over to the right and flashing a red border. Instead of making you think, here you go:

The structure is a bit of navigation that links to sections by ID:

One

Pellentesque habitant morbi ...

Two

Pellentesque habitant morbi ...

Three

Pellentesque habitant morbi ...

When the sections become in :target, the scoot over to the right a bit via translateX transform (prevents any weird text wrapping or anything we might get with padding) and a red border flashes on via keyframe animation.

:target { -webkit-animation: highlight 1s ease; -moz-animation: highlight 1s ease; -webkit-transform: translateX(20px); -moz-transform: translateX(20px); -ms-transform: translateX(20px); -o-transform: translateX(20px); } @-webkit-keyframes highlight { 0% { border-left-color: red; } 100% { border-left-color: white; } } @-moz-keyframes highlight { 0% { border-left-color: red; } 100% { border-left-color: white; } } section > div {
border-left: 40px solid white; padding: 10px; -webkit-transition: all 0.5s ease; -moz-transition: all 0.5s ease; -ms-transition: all 0.5s ease; -o-transition: all 0.5s ease; padding-right: 50px; margin-left: -20px; }

That's all there is too it really. I'd chalk this up under progressive enhancement, if you're worried about browser support. As in, it's just a nice touch, not vital.

Fighting the Jump!

Let's say you like the idea of using :target for states but dislike the page jumping behavior, you can change the hash link in a URL without a page jump.

Using jQuery, you could target all hash-links, prevent their default behavior, and use pushState (or replaceState, I suppose) to change the URL (which won't move the page).

$("a[href^=#]").on("click", function(e) {
e.preventDefault(); history.pushState({}, "", this.href); });

You could interchangeably use replaceState there too, which would change the URL without adding an entry to the browser history. Sometimes you might want that, sometimes you might not. At least you have a choice here, which you don't with the default behavior of clicking a hash link, which always adds.

But there is bad news

When the URL changes to a new hash, you'd think the current target would change and new CSS would take effect. It doesn't (tested current WebKit and Firefox at time of this writing).

Theoretically, you could measure and save the current scroll position of the page, let the link move it naturally, then set it back to where it was. But that just sounds so awful I couldn't even bring myself to make a test page for it.

转载地址:http://xxnel.baihongyu.com/

你可能感兴趣的文章
精读《如何编译前端项目与组件》
查看>>
[ 逻辑锻炼] 用 JavaScript 做一个小游戏 ——2048 (详解版)
查看>>
【产品】产品经理常用的五大分析法
查看>>
Javascript二进制运算符的一些运用场景
查看>>
常见Promise面试题
查看>>
一个专科生学习JAVA目标月薪2万是否不切实际?
查看>>
保持ssh的连接不断开
查看>>
Java 高级算法——数组中查询重复的数字之二
查看>>
897-递增顺序查找树
查看>>
nginx配置后重启无效与重启失败
查看>>
【笔记】JavaScript高级篇——面向对象、原型、继承
查看>>
令人头疼的命名
查看>>
Linux 下配置 node + mongodb 环境
查看>>
Oracle数据库merge into的使用,存在则更新,不存在则插入
查看>>
[LeetCode] Integer to English Words
查看>>
mysql,php和js根据经纬度计算距离
查看>>
如何发布第一个属于自己的npm包
查看>>
Git使用入门
查看>>
定义ll命令
查看>>
maven打jar包 没有主属性清单
查看>>