如何在调整屏幕大小时使元素停止移动

时间:2015-11-24 14:13:02

标签: javascript jquery css css3 less

我有一个带有标记/"元素"的背景​​图像的容器。在背景图像上。当我调整屏幕大小时,我需要将标记放在同一个地方。我试图让它响应,但是当我调整窗口大小时,这些元素似乎并不适合他们的位置。

我知道这是因为背景图像会移动,我需要将图像中的所有元素锁定在同一位置。

http://codepen.io/Sonick/pen/huqrl

.container {
    background: url("http://armsonick.zz.mu/Demos/ImageHost/images/1381607643scene1.png") no-repeat scroll center top transparent;
    color: #000000;
    height: 535px;
    margin: 20px auto;
    overflow: hidden;
    position: relative;
    width: 1030px;
}

.dialog {
    background-color: rgba(163, 154, 77, 0.9);
    color: #FFFFFF;
    display: none;
    height: 140px;
    left: 343px;
    line-height: 24px;
    padding: 100px 30px;
    position: absolute;
    text-align: center;
    top: 97px;
    width: 280px;
    z-index: 10;

    -moz-border-radius: 170px;
    -ms-border-radius: 170px;
    -o-border-radius: 170px;
    -webkit-border-radius: 170px;
    border-radius: 170px;
}
.dialog .close {
    color: black;
    background-color: #65683b;
    cursor: pointer;
    font-size: 22px;
    font-weight: bold;
    height: 36px;
    line-height: 36px;
    position: absolute;
    right: 10px;
    top: 60px;
    width: 36px;

    -moz-border-radius: 18px;
    -ms-border-radius: 18px;
    -o-border-radius: 18px;
    -webkit-border-radius: 18px;
    border-radius: 18px;
}
.labels p {
    display: none;
}
.labels div {
    background-color: rgba(203, 189, 58, 0.8);
    color: #FFFFFF;
    display: none;
    height: 50px;
    padding: 30px 0 0;
    position: absolute !important;
    text-align: center;
    text-decoration: none;
    width: 80px;

    -moz-border-radius: 40px;
    -ms-border-radius: 40px;
    -o-border-radius: 40px;
    -webkit-border-radius: 40px;
    border-radius: 40px;
}
.labels > div {
    background-color: rgba(203, 189, 58, 0.8);

    -moz-transition: .3s;
    -ms-transition: .3s;
    -o-transition: .3s;
    -webkit-transition: .3s;
    transition: .3s;
}
.labels div:hover {
    background-color: rgba(128, 128, 128, 0.8);
}
.labels div span {
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 15px solid rgba(203, 189, 58, 0.8);
    bottom: -14px;
    height: 0;
    left: 30px;
    position: absolute;
    width: 0;

    -moz-transition: .3s;
    -ms-transition: .3s;
    -o-transition: .3s;
    -webkit-transition: .3s;
    transition: .3s;
}
.labels div:hover span {
    border-top: 15px solid rgba(128, 128, 128, 0.8);
}
#label1 {
    left: 720px;
    top: 215px;
}
#label2 {
    left: 495px;
    top: 290px;
}
#label3 {
    left: 450px;
    top: 115px;
}
#label4 {
    left: 270px;
    top: 170px;
}
#label5 {
    left: 570px;
    top: 65px;
}
#label6 {
    left: 275px;
    top: 30px;
}

3 个答案:

答案 0 :(得分:1)

问题在于body的背景 - div元素与其对齐 - 是集中对齐的,而这些div的absolute定位只能起作用从包含div的顶部/左侧。这意味着当容器小于背景图像时,对齐将关闭。

相反,您需要将背景图像与div元素对齐到相同的锚点,以使其工作; top / left

body {
    background: url(http://pcvector.net/uploads/demo/scripts/layout_and_interface/isometric_interactive/images/scene.jpg) left top no-repeat;
}

Updated example

答案 1 :(得分:0)

问题是您将.container元素设置为固定宽度(1030px)。因此,在小于1030px的屏幕上,元素不会响应视口。

将您的.container更改为max-width:1030px;而不是width:1030px

http://jsfiddle.net/ggChris/jjk52j7q/

答案 2 :(得分:0)

要创建自适应设计,您必须在css属性中使用%而不是px

我的意思是,你有这个:

#label1 {
    left: 720px;
    top: 215px;
}

但它应该是:

#label1 {
    left: 69%;
    top: 40%;
}

希望它有所帮助。

相关问题