gpt4 book ai didi

CSS - 在主体内定位带有文本的 DIV 元素

转载 作者:行者123 更新时间:2023-11-28 14:21:15 25 4
gpt4 key购买 nike

我正在修改现有的 html/javascript 包以用作 iPhone 锁屏。我希望它看起来像这样:

alt text

我决定在 HTML 中添加 CSS 和 Javascript,以便您可以立即对其进行测试:

<html>
<head>

<script language="Javascript">
var now = new Date ( );

function getClock ( )
{
var hours = now.getHours ( );
var minutes = now.getMinutes ( );
minutes = ( minutes < 10 ? "0" : "" ) + minutes;

var daypart = ( hours < 12 ) ? "AM" : "PM";
hours = ( hours > 12 ) ? hours - 12 : hours;
hours = ( hours == 0 ) ? 12 : hours;

var clock = hours + ":" + minutes + " " + daypart;

document.getElementById("clock").firstChild.nodeValue = clock;
}

function getCalendar ( )
{
var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December")

var date = now.getDate()
var month = now.getMonth()
var day = now.getDay()

document.getElementById("date").firstChild.nodeValue = date;
document.getElementById("month").firstChild.nodeValue = months[month].substring(0, 3);
document.getElementById("day").firstChild.nodeValue = days[day];
}

</script>

<style type="text/css">
body { font-family: Calibri; color: #fff; background-color: #000; position: absolute; }
#clock { width: 300px; position: absolute; top: 50%; margin: -.7em 0 0 20px; font-size: 53px; text-shadow: -1px 0 #585858, 0 1px #585858, 1px 0 #585858, 0 -1px #585858;}
#day { font-size: 0.9em; line-height: 35px; }
#month { position: absolute; right: 0px; font-size: 1.91em; line-height: 99px; }
#date { position: absolute; left: -125px; top: 0px; width: 102px; text-align: right; }
</style>
</head>

<body onload="getClock(); setInterval('getClock()', 1000 ); getCalendar(); setInterval('getCalendar()', 1000 )">

<div id="background">
<img src="http://dc463.4shared.com/img/32MahG4Y/s7/0.15818551454745688/Background.jpg" width="320" height="480">
</div>

<div id="clock">
<div id="day">
</div>
<div id="month">
<div id="date">
</div>
</div>
</div>
</div>

</body>
</html>

到目前为止,我有两个 Unresolved 问题:

  1. 与示例图片一样,我希望月份几乎触及屏幕的右边缘,同时保持相同数量的左边距。这只能意味着一件事:调整字体大小以使其适合。这可能必须用 Javascript 完成。
  2. 我现在使用的文本边框(使用 text-shadow)非常难看,如果您将我的图片与样本图片进行比较,您会发现边框完全不同!有没有办法让它们更流畅一点?

我希望任何人都可以就如何解决这些问题给出一些想法。提前致谢!

最佳答案

首先,将所有元素包含在一个 div 中并相对定位。这将有助于保持其他内部 div 的相应位置。

<body onload="getClock(); setInterval('getClock()', 1000 )">
<div id="clock">
11:06 AM
<div id="date">
Sunday
</div>
<div id="month">
<script language="JavaScript">getCalendar(); setInterval('getDate()', 1000 )</script>
<script language="JavaScript">getCalendar(); setInterval('getDate()', 1000></script>
</div>
</div>
</body>

请注意,我已经删除了#day div,不需要它我还将所有内容都包含在#clock div 中,以便所有元素都基于#clock 位置定位。如果您需要为脚本返回#day div,只需将它添加到脚本周围的 html 中,不要在 CSS 中设置样式。

CSS(字体大小是根据您链接的示例图片猜测的)......

#clock { font-size: 40px; position: relative; top: 50%; margin: -.5em auto 0 auto; }
#date { position: relative; text-align: left; margin: 0 0 0 40px; }
#month { float: right; font-size: 80px; margin: 0 20px 0 0;}

如果有帮助,这里有一个复制/粘贴 HTML 文件,您可以使用它。请注意,我添加了 body CSS 和 #myscreen div 来模仿手机屏幕的大小。手机上的实际部署不需要它们。

<html>
</head>
<style type="text/css">
body { text-align: center; font-family: verdana; background: #fff; color: #fff;}
#myscreen { position: relative; width: 320px; background: #333; height: 480px; }

#clock { font-size: 40px; position: relative; top: 50%; margin: -.5em auto 0 auto; }
#date { position: relative; text-align: left; margin: 0 0 0 40px; }
#month { float: right; font-size: 80px; margin: 0 20px 0 0;}
</style>
</head>



<body onload="getClock(); setInterval('getClock()', 1000 )">
<div id="myscreen">
<div id="clock">
11:06 AM
<div id="date">
Sunday
</div>
<div id="month">
29 <script language="JavaScript">getCalendar(); setInterval('getDate()', 1000 )</script>
Aug <script language="JavaScript">getCalendar(); setInterval('getDate()', 1000></script>
</div>
</div>
</div>
</body>
</html>

减少或增加 #clock div 的 -.05em 边距将使整个元素 block 向上或向下移动。

关于CSS - 在主体内定位带有文本的 DIV 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8458537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com