当您访问帖子并且答案的橙色背景淡出时,我尝试实现类似于StackOverflow效果的效果。 例: How do I add a class to a given element?
我的方法看起来像这样:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Highlight element based on URL hash example</title>
<style>
/*
Custom class to be applied to our element.
*/
.focused {
padding: 5px;
border-radius: 30px;
box-shadow: 0px 0px 30px #9FC43E;
animation: fade-out-in 5s;
animation-fill-mode: forwards;
}
/*
Add keyframes to our fade out effect
*/
@keyframes fade-out-in {
0% {
border-radius: 30px;
box-shadow: 0px 0px 30px #9FC43E;
}
100% {
border-radius: 0px;
box-shadow: 0px 0px 0px transparent;
}
}
</style>
</head>
<body>
<section style="max-width: 768px">
<div id="target">
This element will be highlighted on load.
</div>
</section>
<script>
// Highlight the target based on the URL
var anchor_id = window.location.href.split('#');
var target = document.getElementById(anchor_id['1']);
target.className += ' focused';
</script>
</body>
</html>
这适用于第一次访问,但如果用户在同一页面上单击新锚点,或者更改地址栏中的URL片段,则该类仍保留在旧元素上。
每当网址改变时,我都在寻找一种方法来移动它。
提前致谢。