简体   繁体   English

使用引导程序将所有对象对齐到div的中间

[英]Align all object in the middle of a div using bootstrap

I'm hoping someone can help me, here's a jsfiddle below. 我希望有人可以帮助我,这是下面的一个jsfiddle。

http://jsfiddle.net/dcsKt/4/ http://jsfiddle.net/dcsKt/4/

here's the js: 这是js:

$(document).ready(function() {
var originalFontSize = 12;
var sectionWidth = $('.web-message').width();

$('.web-message span').each(function(){
    var spanWidth = $(this).width();
    var newFontSize = (sectionWidth/spanWidth) * originalFontSize;
    $(this).css({"font-size" : newFontSize, "line-height" : newFontSize/1 + "px"});
});
});

If you change any of the text, it keeps aligned with the left and right which is perfect, but the trouble i'm having is that when you make the screen smaller. 如果您更改任何文本,它都会与左右对齐,这是完美的选择,但是我遇到的麻烦是当您缩小屏幕时。

I want it to gradually get smaller and stay in the middle of the container, or at worst get smaller by using @media 我希望它逐渐变小并停留在容器的中间,或者最坏的情况是使用@media变小

Any idea? 任何想法?

If I'm understanding correctly, you just need to use $(window).resize . 如果我理解正确, $(window).resize需要使用$(window).resize See here: 看这里:

http://jsfiddle.net/dcsKt/5/ http://jsfiddle.net/dcsKt/5/

$(window).resize(function() {
    var originalFontSize = 12;
    var sectionWidth = $('.web-message').width();

    $('.web-message span').each(function(){
        var spanWidth = $(this).width();
        var newFontSize = (sectionWidth/spanWidth) * originalFontSize;
        console.log(newFontSize);
        $(this).css({"font-size" : newFontSize+"px", "line-height" : newFontSize/1 + "px"});
    });
});

Is that what you're looking for? 那是您要找的东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM