【问题标题】:Enlarging images with CSS (or jQuery) without moving everything else on the page使用 CSS(或 jQuery)放大图像而不移动页面上的所有其他内容
【发布时间】:2012-04-25 21:09:19
【问题描述】:

我有一组动态生成的图像(每张图像附近都有评论)。我想显示最大宽度和最大高度为 48 像素的每个图像,但是当用户将鼠标悬停在图像上时,它会增长到最大宽度和最大高度 200 像素。但无需移动页面上的其他所有内容。这是生成图像的代码:

$(function(){
        $.getJSON("inc/API.php", 
        {command : "getUserComments", userid : getQueryStringValue("userid")},
        function(result)
        {
        for (var i = 0; i<6; i++){
            $("#divUserCommentsContent").append("<div id='divUserComment'><div id='divCommentTime'>"+
            result[i].commentDateAndTime+"</div><div id='divUserDetail'><div id='divUserpic'>
       **<img src='images/"+result[i].imageFileName+"' class='commentpic' />**</div>
            <div id='divUsername'>Comment for <a href='rank.html?imageID="+result[i].imageID+
            "&imageFileName="+result[i].imageFileName+"'>"+result[i].imageHeader+
            "</a></div></div><div id='divCommentText'>"+result[i].comment+"</div></div>");
        }
    });
});

我用 2 ** 标记了图像。这是 CSS:

.userpic, .commentpic
{
max-height:48px;
max-width:48px;
border-radius:5px;
z-index:1;
position:relative;
}
.commentpic:hover
{
max-width: 200px;
max-height: 200px;
position:relative;
z-index: 50;
}

它会放大图像,但也会移动图像右侧和图像下方的所有其他内容。

如何在不移动其他所有内容的情况下使用 CSS(最好)或使用 jQuery 使图像更大?谢谢!

【问题讨论】:

标签: jquery css image image-enlarge


【解决方案1】:

您可以尝试将 .commentpic 的位置设置为绝对位置。

您可以在此处查看示例:http://jsfiddle.net/HkPCp/3/

我认为这是你想要的行为,对吧?

编辑:我更新了 jsFiddle,使其不会移动其他元素。

这是正确的行为吗?

【讨论】:

  • 成功了!图像下方的所有内容(其他 div)都没有移动,但它右侧的 div(divUsername)仍然向左移动(图像下方),所以我也将它的位置设置为绝对,它只是美好的!非常感谢!
  • 干得好。如果我知道图像右侧有内容,我会提供更好的解决方案。但是你已经全部解决了。不错。
  • 当我运行你的测试时,第 3 个盒子会继续移动:第 2 个盒子的悬停。
  • 我编辑了 jsFiddle 来解决这个问题。我似乎将我更新的小提琴保存到其他网址。我会再次更新它。在那里。
  • 有趣的是,这种方法不适用于所有浏览器——在 Safari 和 Chrome 中,图像根本不会增长。它在 FF、Opera 甚至 IE 中运行良好。有什么办法解决吗?
【解决方案2】:

将您的每张图片放入一个具有固定宽度和高度的容器中。您的容器的溢出将是可见的。

任何流出的东西都是可见的,但不会影响内容。

http://jsfiddle.net/ck6MG/

【讨论】:

    【解决方案3】:

    这里会使用 jQuery:

    <div id="enlarge_img"></div>
    
    <style type="text/css">
       div#enlarge_img {
           display: none;
           height: 200px;
           line-height: 200px;
           position: absolute;
           text-align: center;
           width: 200px;
       }
    
       div#enlarge_img > img {
           max-height: 200px;
           max-width: 200px;
       }
    </style>
    
    <script type="text/javascript">
    $(function() {
        $('body img').bind('mouseover', function() {
            var offset = $(this).offset();
    
            $('div#enlarge_img').show().css({
                'left' : offset.left - ((200 - $(this).width()) / 2),
                'top' : offset.top- ((200 - $(this).height()) / 2),
            }).html('<img src="'+$(this).attr('src')+'" />');
        });
    
        $('div#enlarge_img > img').live('mouseout', function() {
            $('div#enlarge_img').hide();
        });
    });
    </script>
    

    基本上你有一个absolutediv,它是隐藏的,直到你悬停一个img,然后将自己定位到img所在的位置并显示相同的图像,但更大。 图像从中心增加,不会影响结构的其余部分,因为它是absolute

    如果你愿意,我可以添加动画。

    【讨论】:

      【解决方案4】:

      https://jsfiddle.net/dafemtg1/

      仅供参考,CSS 使用 transform 属性:

      img{transition:all 0.2s;}
      img:hover {transform:scale(1.4);transition:all 0.2s;}
      

      【讨论】:

        【解决方案5】:

        对你不想移动的元素尝试 css position:absolute

        z-index 放在你想在顶部显示的元素上

        【讨论】:

          【解决方案6】:

          将 class="userpic-container" 添加到图像的父 div 并尝试使用此 CSS:

          .userpic-container {
              position: relative;
              height: 48px;
              width: 48px;
          }
          
          .userpic, .commentpic {
              border-radius: 5px;
              position: absolute;
              height: 48px;
              width: 48px;
              z-index: 1;
          }
          .commentpic:hover {
              height: 200px;
              width: 200px;
              z-index: 10;
          }
          

          【讨论】:

            猜你喜欢
            • 2015-05-28
            • 1970-01-01
            • 1970-01-01
            • 2013-09-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-04
            • 1970-01-01
            相关资源
            最近更新 更多