Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将图像定位为中心,甚至调整窗口大小_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何将图像定位为中心,甚至调整窗口大小

Javascript 如何将图像定位为中心,甚至调整窗口大小,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我试图将图像作为屏幕的中心,就像计算一样 var wh = jQuery(window).innerHeight(); var ww = jQuery(window).innerWidth(); var fh = jQuery('.drop').innerHeight(); var fw = jQuery('.drop').innerWidth(); 我不确定我在哪里犯了错误。例如,请按照我的尝试调整窗口大小 jQuery(wind

我试图将图像作为屏幕的中心,就像计算一样

       var wh = jQuery(window).innerHeight();
        var ww = jQuery(window).innerWidth();
        var fh = jQuery('.drop').innerHeight();
        var fw = jQuery('.drop').innerWidth();
我不确定我在哪里犯了错误。例如,请按照我的尝试调整窗口大小

jQuery(window).resize(function() {
                var wh = jQuery(window).innerHeight();
                var ww = jQuery(window).innerWidth();
                var fh = jQuery('.drop').innerHeight();
                var fw = jQuery('.drop').innerWidth();
            });
但是,即使我调整窗口大小,上面的脚本也不会有帮助,因为图像没有对齐到中心

是否有一种方法可以将图像显示为中心,即使我调整了窗口的大小


这是

你应该试一下

$(window).resize(function () {
    $('.drop2').css({
        position: 'absolute',
        left: ($(window).width() - $('.drop2').outerWidth()) / 2,
        top: ($(window).height() - $('.drop2').outerHeight()) / 2,
    });
});

你应该像这样尝试

$(window).resize(function () {
    $('.drop2').css({
        position: 'absolute',
        left: ($(window).width() - $('.drop2').outerWidth()) / 2,
        top: ($(window).height() - $('.drop2').outerHeight()) / 2,
    });
});

我在我的项目中就是这样做的

.side {
height: 357px;
background-color: red;
overflow: hidden;
position: relative;
}
.middle {
position: absolute;
left: 50%;
margin-left: -250px;
background:url('a.jpg') no-repeat;
width:500px;
height:357px;
}


<div class="side">
<div class="middle"></div>
.side{
高度:357px;
背景色:红色;
溢出:隐藏;
位置:相对位置;
}
.中{
位置:绝对位置;
左:50%;
左边距:-250px;
背景:url('a.jpg')不重复;
宽度:500px;
高度:357px;
}

我在我的项目中就是这样做的

.side {
height: 357px;
background-color: red;
overflow: hidden;
position: relative;
}
.middle {
position: absolute;
left: 50%;
margin-left: -250px;
background:url('a.jpg') no-repeat;
width:500px;
height:357px;
}


<div class="side">
<div class="middle"></div>
.side{
高度:357px;
背景色:红色;
溢出:隐藏;
位置:相对位置;
}
.中{
位置:绝对位置;
左:50%;
左边距:-250px;
背景:url('a.jpg')不重复;
宽度:500px;
高度:357px;
}

您可以单独使用css来实现这一点:

div{
  background: red;
  bottom: 0;
  height: 100px;
  left: 0;
  margin: auto;
  position: absolute;
  top: 0;
  right: 0;
  width: 100px;
}

您可以单独使用css来实现这一点:

div{
  background: red;
  bottom: 0;
  height: 100px;
  left: 0;
  margin: auto;
  position: absolute;
  top: 0;
  right: 0;
  width: 100px;
}

你可以用css来做这件事。你在找类似的东西吗??-@VickyI没有看到你在你的小提琴中使用了(窗口).resize()。。谢谢我从小提琴中删除了resize这个问题。你应该提到你只需要在脚本中使用,或者CSSU可以用css来做。你在找类似的东西吗??-@VickyI没有看到你在小提琴中使用了(window).resize()。。谢谢我从小提琴中删除了resize这个问题。你应该提到,你只需要在Script或csst中使用它。只有当你知道要居中的元素的确切高度/宽度时,它才起作用,哪个OP在创建元素时看起来不是动态的只有当您知道要居中的元素的确切高度/宽度时,此选项才起作用,哪个OP在动态创建元素时看起来不是