将Div锁定在有背景的固定位置

Lock Div to fixed position with background

本文关键字:定位 位置 背景 Div 锁定      更新时间:2024-01-25

即使我们调整浏览器的大小,我也试图将黄色div元素始终保留在home窗口上,但我无法仅用css实现,因此正在寻找JavaScript解决方案。

这是我的小提琴:http://jsfiddle.net/jj4fxpmy/6/

问题是,当我调整浏览器大小时,黄色方块向上移动。我想把它粘在家里。对我来说,保持背景位置在底部是很重要的:

<div class="background"></div>
<div style="position: fixed; background: none repeat scroll 0% 0% gold; top: 50%; z-index: 2147483647; height: 50px; width: 50px; left: 79%;" id="square"></div>
.background{width:100%;height:100%;top:0%;left:0%;position:fixed;
 background:url(http://wallpapers.7savers.com/seasons---summer-wallpapers_1146_1920x1200.jpg) no-repeat; background-color:transparent; background-position:right bottom; background-size:100%;

任何帮助都将不胜感激。

只需CSS即可实现,无需添加js。使用image标记代替背景,并在其中添加黄色div。您可以参考下面的代码。

请参阅HTML

  <img src="http://wallpapers.7savers.com/seasons---summer-wallpapers_1146_1920x1200.jpg" alt="">
  <div>
  <div class="yellow-div" id="square"></div>
  </div>
</div>

参见CSS

.background{
  width:100%;
  height:auto;
  top:0%;
  left:0%;
  position:fixed;
}
.background img{
width:100%;
}
 .yellow-div{
background: none repeat scroll 0 0 gold;
    height: 3.1%;
    position: absolute;
    right: 18.1%;
    top: 68.9%;
    width: 1.6%;
}

你可以参考http://jsbin.com/pacewepari/2/edit?html,css,输出

如果你绝对必须将房屋图像设置为右下角的背景,你可以试着参考这个问题:jQuery-拖动div css背景或者试试这个插件:https://github.com/kentor/jquery-draggable-background

这是我的疯狂想法:

  1. 创建一个与房屋图像尺寸相同的透明图像,带有一个50x50的黄色框
  2. 将其设置为与房屋背景具有相同样式/定位的背景
  3. 将其与引用的溢出问题或Kentor的jquery插件中的jquery连接起来

这样就可以了!