【问题标题】:Why my text is not responsive?为什么我的文字没有响应?
【发布时间】:2014-04-28 03:31:28
【问题描述】:

我使用responsiveslides 库来获得响应式滚动图像。 所以,我希望利用同一个库来获取响应式滚动文本,我编写了这段 JS 代码来计算字体高度,但它不起作用:

function responsiveText()
{
    $('#footer').css('font-size', $('#footer').css('height'));
}

你可以看到我的失败结果here

【问题讨论】:

    标签: javascript responsive-design font-size responsive-slides


    【解决方案1】:

    您可能需要考虑一些事情:

    1) 设置字体大小的代码只会被调用一次

    2) 您正在为孔容器设置字体大小,为什么不直接在 css 中进行呢?

    <style>
    .rslides li {
        font-size: 30px;
    }
    </style>
    

    3) 但是您希望它具有响应性,对吗?然后,您必须考虑容器的字符数和宽度,将其乘以某个值,将该值设置为文本元素的字体大小,并对您拥有的每个文本元素执行此操作。或者不完全是,看看这段代码:

    所以是这样的:

     function responsiveText() {
    
        var ratio = 1/2; // character width/height
        var padding = 10;
        var width = $("#newsSlider").outerWidth()-padding*2;
        var height = $("#footer").outerHeight()-padding*2;
    
        $("#newsSlider").children().each(function () {
            var li = $(this); // one text element/list item
            var len = li.text().length; // number of characters
            var fw = width/len; // maximal character width = width/number of characters
            var fh = height; // maximal character height
    
            // We want fw/fh be so close to ratio as possible by changeing nr of lines
            var nrofl = 1;
            for (; nrofl<5; nrofl++) {
                var nnrofl = nrofl+1;
                var newRatio = (width/(len/nnrofl)) / (height/nnrofl);
                if (Math.abs(newRatio-ratio)<Math.abs(fw/fh-ratio)) {
                    nrofl = nnrofl;
                    fw = (width/(len/nrofl));
                    fh = (height/nrofl);
                } else break;
            }
    
            // One thing's missing if you want a perfect result:
            // Go through the text and insert new lines to make sure
            // there will be nrofl lines. Not going to do it here though.
    
            // Fontsize is min of fw/ratio and fh
            var fs = Math.min(fw/ratio, fh);
    
            // For testing only
            //console.log(Math.round(fs), ratio, Math.round(fw), fh, width, height, nrofl, len);
    
            // Do not forget to round it up to integer value
            li.css('font-size', Math.round(fs)+'px');
        });
    
    }
    
    $(function() {
    
        // image slideshow
        $("#imageSlider").responsiveSlides({
            timeout: 20*1000,
        });
    
        // news slideshow
        $("#newsSlider").responsiveSlides({
            timeout: 5*1000,
        });
    
    
        // resposive news 
        $(window).resize(function(){
            responsiveText();   
        });
        responsiveText();
    
    });
    

    尝试用这个替换旧代码。 (编辑)现在它应该工作得很好!测试了一段时间,尝试调整浏览器大小!剩下的唯一事情(虽然不是关键)是确保会有正确数量的行。祝你好运!

    【讨论】:

    • 您好 Leonard,我尝试应用您的解决方案,但正如您在更新页面中看到的那样,结果几乎相同。有些新闻被剪掉了,不可读
    • 嗨,我从页脚 div 中删除了“填充”,现在所有新闻都可以阅读了。所以,我意识到一些短消息的字体尺寸很小。为什么?
    • 是的,正如我所说,值(50、20 和 30)只是示例。现在,width/len 的值通常小于 20,因此字体大小为 30。但是,较短的文本将获得更大的值,如果大于 20 但小于 30,则会导致更小的值字体大小。所以,是的,我的示例值并不是那么完美..
    • 哦,我忘了对大小进行四舍五入,css 只需要整数作为 px 值:Math.round(fs)+'px'
    • 在您给我的示例站点w3bcreation.it/rpi/stations/testNews 中,在 /rpi/js/call_responsiveSlides.js 中,将整个文件内容替换为上面的代码。此外,在“One thing's missing..”评论中,执行类似 txt = li.text();arr = txt.split(" "); idx = Math.round(arr.length/nrofl);newtxt="";for (i = 0; i";}li.html(newtxt);解决线路问题..
    猜你喜欢
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多