我如何使用一段代码而不重写它

how do i use a block of code without rewriting it over again

如何重用 jquery 代码块而无需重新重写它 例如我得到了这个动画并且想在另一行中重复它

    $("#sign_up_btn").mouseenter(function(){
        $("#sign_up_btn").animate({ opacity: "0.5" }, 300);
        $("#sign_up_btn").animate({ fontSize: "200%" }, 300);
      });
    $("#sign_up_btn").mouseleave(function(){
        $("#sign_up_btn").animate({ opacity: "0.2" }, 300);
        $("#sign_up_btn").animate({ fontSize: "100%" }, 300);
      });

创建函数。

同样 DRY(不要重复自己)

const anim = function(e) {
  const enter = e.type === "mouseenter";
  $(this)
    .animate({ opacity: enter ? "0.5" : "0.2"}, 300)
    .animate({ fontSize: enter ? "200%" : "100%" }, 300); // can possibly be merged
};

$("#sign_up_btn").on("mouseenter mouseleave", anim);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="sign_up_btn">Sign up</button>