简体   繁体   English

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

[英]CSS :hover to force element offset cross-browser solution needed

I have a rather interesting problem, and wondered if it could be done purely in CSS.我有一个相当有趣的问题,想知道它是否可以纯粹在 CSS 中完成。 I know you can use webkit moz transforms, or javascript, but is there another simple CSS cross browser solution to this?我知道您可以使用 webkit moz 转换或 javascript,但是还有另一个简单的 CSS 跨浏览器解决方案吗?

I have a box with a class container say我有一个装有 class 容器的盒子说

.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.现在我想做的是在鼠标悬停(CSS悬停)上将框放大到30px x 30px,围绕10x10框的近似中心某处的中心点,当我鼠标移出时返回到原始容器。 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.现在困难在于topLeft位置设置为从数据库发送的 HTML,因此不会出现在 CSS 文件中。

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. 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不是很优雅。 It's what I'm doing now.这就是我现在正在做的事情。 eeeugh.哎呀。

So come on guys is there a better CSS solution?那么来吧,有没有更好的 CSS 解决方案?

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/演示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.重要的计算是余量需要与 hover 上发生的尺寸增加的一半相匹配。

so, 30 pixel final size minus 10 pixel initial size is 20 pixels.因此,30 像素的最终尺寸减去 10 像素的初始尺寸为 20 像素。 You need to offset ( using margins ) 20/2 = 10 pixels.您需要偏移(使用边距)20/2 = 10 像素。

It is my opinion that Javascript is the most elegant solution.我认为 Javascript最优雅的解决方案。

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 .这本身不是您的问题的答案“是否有更好的 CSS 解决方案”,但我认为 Javascript更好的解决方案 A CSS solution would be hacky at best, and probably wouldn't work. CSS 解决方案充其量只是 hacky,并且可能无法正常工作。

You could override the top and left styles being sent with the HTML by using !important in your stylesheet .您可以通过在stylesheet中使用!important来覆盖与HTML一起发送的topleft styles 。

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

How about wrapping .container in a parent element positioned with the left and top values from your database?.container包装在使用数据库中的lefttop值定位的父元素中怎么样?

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.鉴于该标记,您无需明确了解:hover.container的影响的lefttop ,因此您可以使用固定值,因为它们将根据.container的偏移父级的lefttop确定。

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.您需要 position .container-parent使用您的topleft数据库值。 From there, your CSS would look like:从那里开始,您的 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 */
}

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 .在这里, .container看起来就像通常在数据库的lefttop坐标处一样,但是在:hover上,它会lefttop移动 -15px。 Since it is inside a positioned parent, those new left and top values will be equal to your original values - 15px.由于它位于定位的父级内,因此这些新的lefttop值将等于您的原始值 - 15px。

Is that what you're looking to do?那是你想要做的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM