html - 悬停时从左上角移动到右下角

标签 html css

是否可以将元素从左上角移动到右下角
像这样:
enter image description here

#example {
  width: 40px;
  height: 40px;
  background: red;
  position: absolute;
  top: 0;
  left: 0;
  transition: all 300ms ease 0s;
  right: auto;
  bottom: auto;
}

#example:hover {
  right: 0;
  bottom: 0
}
<div id="example"></div>

最佳答案

是...也不是。

问题是一旦你悬停你的 div 就会移动,所以一旦它从光标下方移动就会停止悬停并且悬停状态将失败。

因此,您必须将悬停移动到另一个元素(可能是 body)。

但是你不能将 left 转换到 right(或到/从 auto)所以你必须调整位置具有转换的元素。

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#example {
  width: 40px;
  height: 40px;
  background: red;
  position: absolute;
  top: 0;
  left: 0;
  transition: all 300ms ease 0s;
}
body:hover #example {
  left: 100%;
  top: 100%;
  transform: translate(-100%, -100%)
}
<div id="example"></div>

注意:如果元素的高度/宽度已知,您也可以使用 calc 并避免转换。

body:hover #example {
    left:calc(100% - 40px);
    top:calc(100% - 40px);

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}
#example {
    width:40px;
    height:40px;
    background:red;
    position:absolute;
    top:0;
    left:0;
    transition: all 300ms ease 0s;
}
body:hover #example {
    left:calc(100% - 40px);
    top:calc(100% - 40px);
}
<div id="example"></div>

关于html - 悬停时从左上角移动到右下角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30755158/

相关文章:

css - css 选择器可以作用于呈现的表边界,而不是表 dom 层次结构吗?

javascript - 动画 jQuery 进度条

html - 使用 CSS 去掉表格中的水平边框

javascript - 如何获取 dom-if 或 dom-repeat 中的元素?

javascript - 网格状、多重响应图像

html - 自动在新窗口中打开每个外部链接(html、css)

javascript - 不使用 mysql 或 php 存储用户输入

html - 粘性页脚不在 4K 屏幕底部

css - 可以在 CSS 中一次定位 nth-of-type(x) 列表吗?

javascript - 在 html 文件中加载 Chosen.jquery.js 文件不起作用