使用 CSS 根据一天中的时间更改主页上的图像?
我正在尝试创建以下内容:
我有五个 svg,每个代表一天中的不同时间。早上、中午、下午等。
是否可以根据用户所在位置的时间来更改图像?如果可以,仅使用 CSS 可以实现吗? (到目前为止我只熟悉 HTML 和 CSS)。如果没有,一般来说实现它的最简单方法是什么?
到目前为止我发现的所有帖子或线程都使用其他语言。
TIA
I am attempting to create the following:
I have five svg´s, each of them representing a different time in the day. Morning, Midday, Afternoon, etc.
Is it possible to make the image change depending of what time it is at the users location? If yes, is this possible with CSS only? (I am only familiar with HTML and CSS so far). If not, what would be the easiest way to achieve it in general?
All posts or threads I have found so far used other languages.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在评论中编写的代码有一些不正确的地方,您不需要使用innerHTML,只需使用
document.getElementById("img").src
代替即可。使用 JavaScript 的一个好技巧是将其放在一个函数上,并在页面加载时调用该函数。如果您使用 cpoy 这段代码,它应该可以正常工作:
我希望这对您有用。
You have some things incorrect on the code you wrote in the comment, you dont need to use innerHTML, just use
document.getElementById("img").src
instead of that.One good tip using JavaScript, is to put it on a function, and call to that function when page is loaded. If you use cpoy this code, it should work correctly:
I hope this works for you.
您可以使用 JavaScript 来实现此目的,您可以创建一个新文件 .js,或者只是在 html 中的
标记之间添加 JavaScript。
您应该使用此函数实际获取小时:
然后您只需将获得的小时与您想要的时间进行比较,并更改图像:
在您的 html 中,您必须有一个
您只需要更改 if 语句,并输入您喜欢的时间。
You can use JavaScript for making this, you can create a new file .js, or just add JavaScript in your html, between
<script>
tags.You should take the hour its actually, using this function:
Then you just can compare the hour you get with the time you want, and change the image:
In your html, you must have a
You just will need to change the if statements, and put the hours you prefer.