如何根据当前视口位置将图像置于特定div中?

时间:2013-07-20 13:36:33

标签: css ajax css3 jquery

我正在尝试使用ajax加载内容时创建漂亮的动画。我想在使用"内容"重新加载div时使用显示图标,但我无法弄清楚是否可以仅使用CSS执行此操作。

图标应该:

  1. 横向始终位于div的中心,使用"内容"
  2. 垂直始终位于"内容的可见部分"
  3. 应该在整个动画期间保持在"内容的可见部分"在幻灯片动画中隐藏菜单。
  4. enter image description here enter image description here enter image description here

    如果垂直居中根据"内容的可见部分"是不可能的,根据浏览器的视口中心图像是可以的。

    [编辑]

    这是我的小提琴:http://jsfiddle.net/QWB9x/74/以及可能应该改变的部分:

    .loading #img_loading {
        position: fixed;
        top: 50%;
        left: 50%;
        display: block;
    }
    

5 个答案:

答案 0 :(得分:2)

这对我最有效:)

function loadNewContent(){
 $(".loaderCont").removeClass("loading")
}

$(document).ready(function () {

 $("#hide_button").on("click", function () {
      $(this).closest(".bottom").toggleClass("left_hided");
      $(".loaderCont").toggleClass("left_hided2");
 });

 $("#filter1,#filter2,#filter3,#filter4").on("click", function() {
     $(".loaderCont").addClass("loading");
     setTimeout(loadNewContent, 2000);
 });
});

CSS:

.header {
    background-color: Green;
    width: 100%;
    margin-bottom: 20px;
     height: 100px;
}
.left {
    background-color: Red;
    float: left;
    width: 100px;
}
.left_hided .left{
    margin-left: -85px;
}
.right {
    background: Aqua url("http://i.imgur.com/ifyW4z8.png") 50% repeat-y;
    width: calc(100% - 140px);
    float: right;
}

.left_hided .right{
     width: calc(100% - 55px);
}

input{
    float:right;
}

.loaderCont {
    background-color: rgba(255, 0, 0, 0.6);
    height: 100%;
    width: calc(100% - 140px);
    position: fixed;
    right: 0;
    top: 0;
    z-index: -1;
}

.left_hided2 {
     width: calc(100% - 55px);
}

#loader {
    background: url(http://i.stack.imgur.com/FhHRx.gif) no-repeat center center;
    position: relative;
    top: calc(50% - 16px);
    left: calc(50% - 16px);
    display: block;
     height: 32px;
     width: 32px;
}

.loading {
    z-index: 9001;
}

JSFiddle:http://jsfiddle.net/ZRwzr/1/

答案 1 :(得分:1)

为了满足您的要求,您需要使用JavaScript来确定需要使用固定位置div将加载图像放置在可见部分上的位置,然后在用户调整窗口大小或滚动窗口时重新定位它它始终处于理想的位置。

$(window).scroll(function() {
    scrolling();
});

$(window).resize(function() {
   scrolling(); 
});

scrolling();

function scrolling() {
    var windowHeight = $(window).height();
    var scrollTop = $(window).scrollTop();
    var offsetTop = $('#thediv')[0].offsetTop;
    var offsetHeight = $('#thediv')[0].offsetHeight;

    var offsetWidth = $('#thediv')[0].offsetWidth;

    var top = offsetTop - scrollTop;
    top = top < 0 ? 0 : top;

    var bottom = (scrollTop + windowHeight) - (offsetHeight + offsetTop);
    bottom = bottom < 0 ? 0 : bottom;

    $('.image').css('top', top);
    $('.image').css('bottom', bottom);
    $('.image').css('width', offsetWidth);
}

请注意,如果更改div的宽度,则始终需要调用scrolling()函数,以便重新计算位置。

我还将加载图像作为背景图像添加到固定div中,以便我们可以使用CSS来居中它。

.image {
    position: fixed;
    text-align:center;
    background-image:url('http://i.stack.imgur.com/FhHRx.gif');
    background-repeat:no-repeat;
    background-position:center center;
    background-color:rgba(255,255,255,.4);
}

这是JSFiddle http://jsfiddle.net/QWB9x/89/

答案 2 :(得分:0)

您无需执行大量脚本操作即可为其分配位置。你可以用简单的CSS来完成。

只需使加载程序相对于其父项。指定高度和宽度并执行以下css部分。

.loader {position:relative;顶部:高度的一半;左: - 宽度的一半;边距:50%;保证金左:50%;适用于每个设备

答案 3 :(得分:-1)

使用z-index示例: -

 <div style="z-index:100;">loading image</div> 

答案 4 :(得分:-1)

.loading #img_loading {
    position: fixed;
    top: 50%;
    left: calc(50% + 55px);
    display: block;
}

以上将解决问题的一半,你需要用javascript动态更新左边。