CSS:悬停以强制元素偏移需要跨浏览器解决方案
我有一个相当有趣的问题,想知道是否可以纯粹用 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;
}
现在的困难在于 top
和 Left
位置被设置到从数据库发送的 HTML 中,因此不会出现在 CSS 中文件。
选项是我从数据库数据动态生成 CSS 文件,并有效地为每个对象创建一个类(我真的认为这是一个坏主意),或者我使用 Javascript 来管理一些计算 onmouseover
和onmouseout
但这让我觉得不太优雅。这就是我现在正在做的事情。哎呀。
那么大家有没有更好的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它只是顶部/左侧,那么您可以忽略它们并使用边距来移动它们(假设您知道尺寸)
演示 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)
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.
我认为 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.
您可以通过在
中使用
。!important
来覆盖随HTML
发送的top
和left
样式样式表You could override the
top
andleft
styles being sent with theHTML
by using!important
in yourstylesheet
.如何将
.container
包装在使用数据库中的left
和top
值定位的父元素中?有了这个标记,您就不需要明确知道
left
和top
来实现.container:hover
效果code>,因此您可以使用固定值,因为它们将根据.container
的偏移父级的left
和top
确定。例如,给定此标记:
您需要使用
top
和left
数据库值来定位.container-parent
。从那里,您的 CSS 将如下所示:这里,
.container
看起来就像通常在数据库的left
和top
坐标处一样,但是:hover
将自身向左侧
和顶部
移动-15px。由于它位于定位父级内部,因此这些新的left
和top
值将等于您的原始值 - 15px。这就是你想做的事吗?
How about wrapping
.container
in a parent element positioned with theleft
andtop
values from your database?Given that markup, you wouldn't need to know the
left
andtop
explicitly for your:hover
effect on.container
, so you could use fixed values as they will be determined based on theleft
andtop
of.container
's offset parent.For example, given this markup:
you'd need to position
.container-parent
using yourtop
andleft
database values. From there, your CSS would look like:Here,
.container
appears like it usually would at theleft
andtop
coordinates from your database, but on:hover
shifts itself -15px to theleft
andtop
. Since it is inside a positioned parent, those newleft
andtop
values will be equal to your original values - 15px.Is that what you're looking to do?