javascript - 鼠标悬停时更改字体大小,鼠标离开时恢复为原始字体大小

标签 javascript jquery

我正在尝试编写一个函数,当鼠标进入元素时更改元素的字体大小,并在鼠标离开元素时将其恢复为原始字体大小。这就是我所拥有的:

   $(".month").hover(function(){

        var size = $(this).css("font-size");

        $(this).stop().animate({
            fontSize: start_font + font_off,
            opacity: '1'
        }, 200);    

    },function(){

        $(this).stop().animate({
            fontSize: size,
            opacity: '1'
        }, 200);
    });

鼠标移入时字体大小会改变,但鼠标离开时字体大小保持不变。 (我在字体大小更改后执行了警报(大小),并且它保持了正确的值。)我在这里做错了什么?

最佳答案

您可以使用 CSS 轻松实现此目的:

.month:hover {
  font-size: 150%;
  }

但是,在 jquery 中你可以这样做:

$(".month").hover(function(){
  $(this).
    stop().
    animate({
      fontSize: "5em"
      }, 1000);
  },
  function(){
    $(this).
      stop().
      animate({
        fontSize: "1em"
        }, 1000);
    }
  );

See jsfiddle 。注意,我用过emsThe “em” is a scalable unit that is used in web document media. An em is equal to the current font-size, for instance, if the font-size of the document is 12pt, 1em is equal to 12pt. Source

关于javascript - 鼠标悬停时更改字体大小,鼠标离开时恢复为原始字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16767128/

相关文章:

javascript - 我在模板中使用 vue-router 和 jQuery,当我切换路由器 $(document).ready() 失败时

javascript - JQuery 如何在表单有一些数据数组时只提交更改的字段

javascript - Angular 4 设置 ng-valid 来处理无效的电子邮件输入

javascript - 使用 JSON 从 YouTube channel 获取视频列表

javascript - JQUERY 在多个无序列表中选择第 n 个列表元素

jquery - 多个 jsTree 实例

javascript - 如何在 jquery 中向 'this' 上面的总体类添加一个类

javascript - 将事件附加到文档,这取决于 React 中有条件呈现的元素

javascript - jQuery UI 子菜单中的输入字段不起作用

jquery - 如何在使用自定义 'data' 属性时保留前导零