CSS:悬停以强制元素偏移需要跨浏览器解决方案

发布于 2024-11-09 19:33:39 字数 736 浏览 2 评论 0原文

我有一个相当有趣的问题,想知道是否可以纯粹用 CSS 来完成。我知道你可以使用 webkit moz 转换或 javascript,但是还有另一种简单的 CSS 跨浏览器解决方案吗?

我有一个带有类容器的盒子

.container
{
 position: absolute;
 border: black 1px solid;
 top: 100px;
 left:100px;
 width: 10px;
 height: 10px;
}

现在我想做的是在鼠标悬停(CSS悬停)时将盒子放大到 30px x 30px 围绕 10x10 盒子大约中心的某个中心点,当返回到原始容器时我把鼠标移开。大致如下:

.container:hover
{
 width: 30px;
 height: 30px;
 top: 90px;
 left:90px;
}

现在的困难在于 topLeft 位置被设置到从数据库发送的 HTML 中,因此不会出现在 CSS 中文件。

选项是我从数据库数据动态生成 CSS 文件,并有效地为每个对象创建一个类(我真的认为这是一个坏主意),或者我使用 Javascript 来管理一些计算 onmouseoveronmouseout 但这让我觉得不太优雅。这就是我现在正在做的事情。哎呀。

那么大家有没有更好的 CSS 解决方案呢?

I have a rather interesting problem, and wondered if it could be done purely in CSS. I know you can use webkit moz transforms, or javascript, but is there another simple CSS cross browser solution to this?

I have a box with a class container say

.container
{
 position: absolute;
 border: black 1px solid;
 top: 100px;
 left:100px;
 width: 10px;
 height: 10px;
}

Now what I want to do is on mouseover (CSS hover) is enlarge the box to 30px by 30px around a central point somewhere in the approximate centre of the 10x10 box, returning to the original container when I mouse out. Something along the lines of:

.container:hover
{
 width: 30px;
 height: 30px;
 top: 90px;
 left:90px;
}

Now the difficulty comes in that the top and Left positions are set into the HTML being sent from the database, and therefore do not appear in the CSS file.

The option is that I dynamically generate the CSS file from the DB data, and effectively create a class for every object (I really think this is a bad idea) or I use Javascript to manage some calculation onmouseover and onmouseout but this strikes me as being not very elegant. It's what I'm doing now. eeeugh.

So come on guys is there a better CSS solution?

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

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

发布评论

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

评论(4

冰魂雪魄 2024-11-16 19:33:39

如果它只是顶部/左侧,那么您可以忽略它们并使用边距来移动它们(假设您知道尺寸

.container:hover
{
 width: 30px;
 height: 30px;
 margin-left:-10px;
 margin-top:-10px;
}

演示 http://jsfiddle.net/gaby/CuuNW/


重要的计算是边距需要匹配大小增加的一半悬停时发生。

因此,30 像素的最终大小减去 10 像素的初始大小就是 20 像素。您需要偏移(使用边距)20/2 = 10 像素。

If it just the top/left then you can ignore those and use the margins to move them around (provided that you know the dimensions)

.container:hover
{
 width: 30px;
 height: 30px;
 margin-left:-10px;
 margin-top:-10px;
}

demo http://jsfiddle.net/gaby/CuuNW/


The important calculation is that the margin need to match the half of the size increase that occurs on hover.

so, 30 pixel final size minus 10 pixel initial size is 20 pixels. You need to offset (using margins) 20/2 = 10 pixels.

标点 2024-11-16 19:33:39

我认为 Javascript 最优雅的解决方案。

客户端脚本的目的是用于客户端交互,这正是您想要做的。

这本身并不是您问题“是否有更好的 CSS 解决方案”的答案,但我认为 Javascript更好的解决方案 >。 CSS 解决方案充其量只是很笨拙,而且可能行不通。

It is my opinion that Javascript is the most elegant solution.

The purpose of client-side scripting is for client-side interaction, which is exactly what you're trying to do.

This isn't an answer, per se, to your question "is there a better CSS solution," but I think Javascript is the better solution. A CSS solution would be hacky at best, and probably wouldn't work.

九厘米的零° 2024-11-16 19:33:39

您可以通过在 中使用 !important 来覆盖随 HTML 发送的 topleft 样式样式表

.container:hover
{
 width: 30px;
 height: 30px;
 top: 90px !important;
 left:90px !important;
}

You could override the top and left styles being sent with the HTML by using !important in your stylesheet.

.container:hover
{
 width: 30px;
 height: 30px;
 top: 90px !important;
 left:90px !important;
}
海的爱人是光 2024-11-16 19:33:39

如何将 .container 包装在使用数据库中的 lefttop 值定位的父元素中?

有了这个标记,您就不需要明确知道 lefttop 来实现 .container:hover 效果code>,因此您可以使用固定值,因为它们将根据 .container 的偏移父级的 lefttop 确定。

例如,给定此标记:

<div class="container-parent">
    <div class="container">
        ...   
    </div>
</div>

您需要使用 topleft 数据库值来定位 .container-parent。从那里,您的 CSS 将如下所示:

.container-parent{
    left:224px; /* from DB */
    position: absolute;
    top:128px; /* from DB */
}

.container
{
 position: absolute;
 border: black 1px solid;
 top: 100px;
 left:100px;
 width: 10px;
 height: 10px;
}

.container:hover
{
 width: 30px;
 height: 30px;
 top: -15px; /* new width / 2 to center box */
 left:-15px; /* new height / 2 to keep things centered */
}

这里,.container 看起来就像通常在数据库的 lefttop 坐标处一样,但是:hover 将自身向左侧顶部移动-15px。由于它位于定位父级内部,因此这些新的 lefttop 值将等于您的原始值 - 15px。

这就是你想做的事吗?

How about wrapping .container in a parent element positioned with the left and top values from your database?

Given that markup, you wouldn't need to know the left and top explicitly for your :hover effect on .container, so you could use fixed values as they will be determined based on the left and top of .container's offset parent.

For example, given this markup:

<div class="container-parent">
    <div class="container">
        ...   
    </div>
</div>

you'd need to position .container-parent using your top and left database values. From there, your CSS would look like:

.container-parent{
    left:224px; /* from DB */
    position: absolute;
    top:128px; /* from DB */
}

.container
{
 position: absolute;
 border: black 1px solid;
 top: 100px;
 left:100px;
 width: 10px;
 height: 10px;
}

.container:hover
{
 width: 30px;
 height: 30px;
 top: -15px; /* new width / 2 to center box */
 left:-15px; /* new height / 2 to keep things centered */
}

Here, .container appears like it usually would at the left and top coordinates from your database, but on :hover shifts itself -15px to the left and top. Since it is inside a positioned parent, those new left and top values will be equal to your original values - 15px.

Is that what you're looking to do?

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