使用 CSS 根据一天中的时间更改主页上的图像?

发布于 2025-01-10 19:59:26 字数 340 浏览 1 评论 0原文

我正在尝试创建以下内容:

我有五个 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.

Edit: enter image description here

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈情不如逗狗 2025-01-17 19:59:26

您在评论中编写的代码有一些不正确的地方,您不需要使用innerHTML,只需使用 document.getElementById("img").src 代替即可。

使用 JavaScript 的一个好技巧是将其放在一个函数上,并在页面加载时调用该函数。如果您使用 cpoy 这段代码,它应该可以正常工作:

window.onload = changeImage;

function changeImage() {
    const time = new Date().getHours();

    if (time < 6) {
        document.getElementById("img").src = "img/meerkat_five.svg";
    } else if (time < 9) {
        document.getElementById("img").src = "img/meerkat_one.svg";
    } else if (time < 14) {
        document.getElementById("img").src = "img/meerkat_two.svg";
    } else if (time < 17) {
        document.getElementById("img").src = "img/meerkat_three.svg";
    } else if (time < 20) {
        document.getElementById("img").src = "img/meerkat_four.svg";
    }
}

我希望这对您有用。

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:

window.onload = changeImage;

function changeImage() {
    const time = new Date().getHours();

    if (time < 6) {
        document.getElementById("img").src = "img/meerkat_five.svg";
    } else if (time < 9) {
        document.getElementById("img").src = "img/meerkat_one.svg";
    } else if (time < 14) {
        document.getElementById("img").src = "img/meerkat_two.svg";
    } else if (time < 17) {
        document.getElementById("img").src = "img/meerkat_three.svg";
    } else if (time < 20) {
        document.getElementById("img").src = "img/meerkat_four.svg";
    }
}

I hope this works for you.

相思碎 2025-01-17 19:59:26

您可以使用 JavaScript 来实现此目的,您可以创建一个新文件 .js,或者只是在 html 中的

您应该使用此函数实际获取小时:

<script type=»text/javascript»>

var date = new Date();
var hour = (date.getHours());

</script>

然后您只需将获得的小时与您想要的时间进行比较,并更改图像:

<script type=»text/javascript»>

var date = new Date();
var hour = (date.getHours());

if (hour > 12) {
     document.getElementById("img").src="img/day.png";
}

</script>

在您的 html 中,您必须有一个

<img id="img" src="img/picture.png"> 

您只需要更改 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:

<script type=»text/javascript»>

var date = new Date();
var hour = (date.getHours());

</script>

Then you just can compare the hour you get with the time you want, and change the image:

<script type=»text/javascript»>

var date = new Date();
var hour = (date.getHours());

if (hour > 12) {
     document.getElementById("img").src="img/day.png";
}

</script>

In your html, you must have a

<img id="img" src="img/picture.png"> 

You just will need to change the if statements, and put the hours you prefer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文