Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.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
jquery CSS不';我不能在webkit中工作_Jquery_Css_Webkit - Fatal编程技术网

jquery CSS不';我不能在webkit中工作

jquery CSS不';我不能在webkit中工作,jquery,css,webkit,Jquery,Css,Webkit,如果图像是纵向的,我尝试将一些css应用于图像标记,但由于某些原因.css()在webkit中不起作用。基本上,图像标签没有“style”属性。这是我的密码: $(document).ready(function () { $('.listing .item .thumb img').each(function () { var _img = $(this); var _imgWidth = _img.outerWidth();

如果图像是纵向的,我尝试将一些css应用于图像标记,但由于某些原因.css()在webkit中不起作用。基本上,图像标签没有“style”属性。这是我的密码:

$(document).ready(function () {

    $('.listing .item .thumb img').each(function () {

          var _img = $(this);
          var _imgWidth = _img.outerWidth();
          var _imgHeight = _img.outerHeight();

          if (_imgHeight > _imgWidth) {
               _img.css('top', '-40%');
          }
    });
});

<div class="listing about">
    <div class="item">
        <a href="#" class="thumb">
            <img src="../_uploads/siteassets/images/thumb-carousel-2.jpg" />
            <span class="frame"></span>
        </a>
        <span class="snippet">
            <a href="#" class="title">Name Surname</a>
            <span class="date">Role in the company</span>
            <p class="description">Lorem ipsum dolor sit amet...</p>
        </span>
    </div>
</div>
$(文档).ready(函数(){
$('.listing.item.thumb img')。每个(函数(){
var _img=$(本);
var_imgWidth=_img.outerWidth();
var _imgHeight=_img.outerHeight();
如果(\u imgHeight>\u imgWidth){
_img.css('top','-40%);
}
});
});
在公司中的角色

Lorem ipsum door sit amet

我尝试过使用.attr()而不是.css(),但什么都没有。它不起作用

知道为什么吗?

当启动
.ready()
时,图像可能没有设置尺寸

0>0
为false,因此从不设置
top
属性


改为使用,或。

Webkit不会解释左偏移或顶部偏移,因为默认位置为无,请添加相对位置。mozilla的Gecko在有无相对位置时解释偏移量

    <div style="position: relative; left: 10px;"></div>
    <script>
         alert($('div').offset().left);
    </script>

警报($('div').offset().left);

top
属性仅在元素具有属性
位置
和值
绝对
相对
或静态时才有效。另外,我不确定top是否接受百分比值。图像是绝对定位的,是的,它接受百分比,它适用于除webkit之外的所有其他浏览器。谢谢,它可以工作!我仍然不明白为什么要使用.ready()它在所有其他浏览器中都有效……无论如何,谢谢你的帮助!:)如果您在图像上显式地设置了宽度和高度属性(我建议这样做),那么您的第一个版本也应该可以工作。有时候,你有理由不能或不想这样做,但我想我应该提一下。