【问题标题】:Unable to center the Title Tags on Images无法在图像上居中标题标签
【发布时间】:2014-08-24 22:04:28
【问题描述】:

我试图将图像和它们的帖子标题居中对齐,但不知何故,图像的左侧部分和标题被剪掉了一小部分。我尝试了很多东西,但无法使标题标签居中。 Here is the site 我试图将图片与他们的帖子标题一起居中,我使用的脚本是下面的脚本

<script type='text/javascript'>//<![CDATA[
/**
this script was written by Confluent Forms LLC http://www.confluentforms.com
for the BlogXpertise website http://www.blogxpertise.com
any updates to this script will be posted to BlogXpertise
please leave this message and give credit where credit is due!
**/

$(document).ready(function() {
    // 0change the dimension variable below to be the max pixel width you want the grid elements to be
    // this works better if they're the same size
    var width       = 190;
    var height      = 190;

    // need an image to use in case the blog post does not have an image
    // works best if the image is the same dimensions as the above variable values for grid size
    var placeholder = 'http://lh5.googleusercontent.com/-bY-qeacmAlA/UI_98V9I9WI/AAAAAAAAGWc/8FzFSGYaj3o/s270/placeholder.jpg';

    // grid margins such as 5px
    var margins     = "15px 2px 5px 2px";

    // set 1 for squared image or 0 for proportional,
    // highly recommend squared otherwise grid might not display properly
    // squared images only works for images hosted in Picasa
    var square      = 1;

    // style info for the "alt" area where the post title will be displayed
    var altBackground       = "#000000";
    var altTextColor        = "#ffffff";
    var altPaddingTop       = 15;
    var altPaddingBottom        = 20;
    var altPaddingSides     =5; 

    var paddingString = altPaddingTop + 'px ' + altPaddingSides + 'px ' + altPaddingBottom + 'px ' + altPaddingSides + 'px';

    $('.post-body').each(function(n, wrapper){
        var wrapper         = $(wrapper);
        var image       = $(wrapper).find('img').first();
        var link        = wrapper.parent().find('h3 a');
        var linkURL         = link.attr('href');
        var linkTitle       = link.text();

        $(link).remove();
        wrapper.empty();

        if (image.attr('src')) {
            var imageOriginalHeight = image.attr('height');
            var imageOriginalWidth = image.attr('width');   
            var imageParent = $(image).parent();

            wrapper.append(imageParent);

            if (square) {
                image.attr({src : image.attr('src').replace(/s\B\d{3,4}/,'s' + width + '-c')});
                image.attr('width',width).attr('height',height);
            } else {
                image.attr({src : image.attr('src').replace(/s\B\d{3,4}/,'s' + width)});
                image.attr('width',width);
                var newHeight = (imageOriginalHeight/imageOriginalWidth * width).toFixed(0);
                image.attr('height',newHeight);
            }

        } else {
            var image = $('<img>').attr('src',placeholder).attr('height',height).attr('width',width);
            var imageParent = $('<a>').append(image).appendTo(wrapper);
        }

        imageParent.attr('href',linkURL).css('clear','none').css('margin-left','0').css('margin-right','0').addClass('postImage');

        var imageAlt = $('<div>').prepend(linkTitle).css('padding',paddingString).css('color',altTextColor).css('background-color',altBackground).css('opacity','0.9').css('filter','alpha(opacity=90)').css('width',width).appendTo(imageParent);  //.

        var divHeight = imageAlt.height();
        var sums = parseInt(divHeight) + parseInt(altPaddingTop) + parseInt(altPaddingBottom);
        imageAlt.css('margin-top','-'+sums+'px');           
        wrapper.css('float','left').css('height',height).css('width',width).css('margin',margins).css('overflow','hidden');
    });
    $('#blog-pager').css('clear','both');
});

function killLightbox() {
    var images = document.getElementsByTagName('img');
    for (var i = 0 ; i < images.length ; ++i) {
        images[i].onmouseover=function() {
            var html = this.parentNode.innerHTML;
            this.parentNode.innerHTML = html;
            this.onmouseover = null;
        };
    }
}

if (window.addEventListener) {
    window.addEventListener('load',killLightbox,undefined);
} else {
    window.attachEvent('onload',killLightbox);
}
//]]></script>
<style>
a.postImage div {
    display:    block;
} a.postImage:hover div {
    display:    block;
}
h3, .post-footer {
    display:    none;
}
 h2{display: none;}
</style>

这是修改后的脚本,您可以找到 here 的实际脚本。要测试您的修改,您可以在 chrome 或其他浏览器中使用 Stylebot app 扩展名。提前谢谢了! :)

【问题讨论】:

    标签: javascript jquery html css centering


    【解决方案1】:

    我不确定如何编辑脚本,但通过 chrome 控制台我做了以下修复它。

    1. 在柱体下方移除浮动。
    2. 偏移图像本身。

    文本已经居中,问题是元素的定位。

    看截图 -> 繁荣之地

    编辑

         .postImage img {left: -6px; bottom: 10px;} 
         .post-body a {float:none;}
    

    【讨论】:

    • 嗨@brygiger!我尝试使用此 .postImage{left:15px; 发布图像偏移右:15px; position:absolute;} 并且它起作用了,但现在右侧部分被切断了!对 css 有什么建议吗?
    • “这个”?对不起。 编辑,看到评论的其余部分。给我一秒钟
    • 试试 .postImage img {left: 5px; bottom: 10px;} 你应该移动 img 标签而不是图像中心的父级。 \
    • 是的,它成功了! :D 但第一个图片标题被剪掉了一点!有什么解决办法吗?
    • 好吧,对于文本来说,div 太小了,所以你要么让包含的 div 更大(更高)并换行,要么让文本更小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2020-05-23
    相关资源
    最近更新 更多