javascript - 尝试实现 Fittext.js

标签 javascript jquery html css

我正在尝试在我的大 H1 标签上使用 jQuery,以便在调整浏览器窗口大小时,H1 标签中的文本将保持不变。

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/text.js"></script>
</head>

<body>

<div class="main">
    <section class="page" id="page1">
        <div class="test">
            <h1>exampletext</h1>

CSS:

.test h1 {
    width:100%;
    text-align:center;
    font-size:250px;
    text-transform:uppercase;
    letter-spacing:5px;
}

JS:

$( document ).ready(function() {
    $("test h1").fitText(1, { minFontSize: '20px', maxFontSize: '250px' });
});

(function( $ ){

  $.fn.fitText = function( kompressor, options ) {

    // Setup options
    var compressor = kompressor || 1,
        settings = $.extend({
          'minFontSize' : Number.NEGATIVE_INFINITY,
          'maxFontSize' : Number.POSITIVE_INFINITY
        }, options);

    return this.each(function(){

      // Store the object
      var $this = $(this);

      // Resizer() resizes items based on the object width divided by the compressor * 10
      var resizer = function () {
        $this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
      };

      // Call once to set.
      resizer();

      // Call on resize. Opera debounces their resize by default.
      $(window).on('resize.fittext orientationchange.fittext', resizer);

    });

  };

})( jQuery );

当我调整浏览器窗口大小时它似乎不起作用,我做错了什么?

最佳答案

这一行

$("test h1").fitText(1, { minFontSize: '20px', maxFontSize: '250px' });

应该是

$(".test h1").fitText(1, { minFontSize: '20px', maxFontSize: '250px' });

你在查询选择器中漏掉了一个句号

https://jsfiddle.net/craigiswayne/cp5at05y/

关于javascript - 尝试实现 Fittext.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46849744/

相关文章:

javascript - 制作新圆标记时删除圆标记

Javascript 匹配字符串内的括号

javascript - VueJS - 将标记中的数据发送到 Vue 实例以在 mounted() 中使用

javascript - 如何按特定金额拆分数字

javascript - 如何在KML谷歌地图中显示infoWindow?

html - 如何允许 g-wan 提供预格式化的文本?

jquery - 使用 jQuery AJAX 获取请求页面的标题

javascript - 如何将 fancybox 与 filterizr 一起使用,以便只有过滤后的项目出现在图片库中?

javascript - Snap.svg 加载的 SVG 图形的主要问题 - 在特定元素上选择和动画路径变换不起作用

html - 仅更改文本的前 6 个字符的颜色 css