Javascript 无法在图像上居中放置标题标记

Javascript 无法在图像上居中放置标题标记,javascript,jquery,html,css,centering,Javascript,Jquery,Html,Css,Centering,我试着将图片和它们的文章标题对齐,但不知怎么的,图片和标题的左侧部分被一小段边距切断了。我尝试了很多方法,但都无法将标题标记居中。 我试着把图片和他们的文章标题放在中间,我使用的脚本如下 <script type='text/javascript'>//<![CDATA[ /** this script was written by Confluent Forms LLC http://www.confluentforms.com for the BlogXpertise we

我试着将图片和它们的文章标题对齐,但不知怎么的,图片和标题的左侧部分被一小段边距切断了。我尝试了很多方法,但都无法将标题标记居中。 我试着把图片和他们的文章标题放在中间,我使用的脚本如下

<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>

/我不确定如何编辑脚本,但在chrome控制台上,我做了以下操作来修复它

  • 拆下柱体下方的右侧浮子
  • 偏移图像本身
  • 文本已经居中,问题在于元素的定位

    请参见屏幕截图->boomlands

    编辑


    嗨@brygiger!我试着用这个做后期图像偏移。后期图像{left:15px;right:15px;position:absolute;}成功了,但现在右侧部分被切断了!对css有什么建议吗?“这个”?很抱歉编辑,查看其余的评论。给我一个sectry.postImage img{left:5px;bottom:10px;}您应该移动img标记,而不是图像中心的父标记\是的,成功了但是第一张图片的标题被剪掉了一点!有什么解决方案吗?嗯,div对于文本来说太小了,所以你要么把包含div的部分变大(更高),然后把文本包装起来,要么把文本变小。
         .postImage img {left: -6px; bottom: 10px;} 
         .post-body a {float:none;}