在Javascript中设置最大字体大小

时间:2016-05-11 07:08:01

标签: javascript html

快速问题,我怎样才能设置最大字体大小,以便在调整大小时字体不会太大?我的默认字体大小是22px,但使用此代码它会比默认值更高。

<script>
    function resize_text(){
        resize = $(window).width()/50;
        min = 18;

        if(resize > min){
            $('p').css('font-size',resize + 'px'); 
            $('h1').css('font-size',resize + 'px'); 
            $('.bottom-list').css('font-size',resize + 'px'); 
        } else {
            $('p').css('font-size',min + 'px'); 
            $('h1').css('font-size',min + 'px'); 
            $('.bottom-list').css('font-size',min + 'px');
        }
    }
    $(window).resize(function(){
        resize_text();
    })

    </script>

3 个答案:

答案 0 :(得分:0)

您正在使用 resize = $(window).width()/ 50; ,其中resize值可能超过22。 如果您的默认值为22,则可以尝试以下代码:

resize = 22;
if($(window).width()/50 < 22){
     resize = $(window).width()/50
}

答案 1 :(得分:0)

不是用javascript做这个,你不是用css吗?

p{font-size: 100%} or provide anyother percentage criteria as per your requirement.

我认为它会解决您的问题。

答案 2 :(得分:0)

您也可以更改newsize = $(window).width()/50; resize = newsize > 22 ? 22 : newsize; 定义的行:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String DB_URL="jdbc:mysql://localhost/Demo";
    String USER = "root";
    String PASS = "root";

       String Id=request.getParameter("e1");
       int id = Integer.parseInt( Id );
       String pos=request.getParameter("p1"); 
       String pr1=request.getParameter("e1");
       int pri = Integer.parseInt( pr1 );

       response.setContentType("text/html");
       PrintWriter out = response.getWriter();

      try{ Class.forName("com.mysql.jdbc.Driver");
           Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
           PreparedStatement ps=conn.prepareStatement("select * from users where id=?");
           ps.setInt(1, id);
           ResultSet rs=ps.executeQuery();
            while(rs.next()){
            int eid  = rs.getInt("id");
            String post = rs.getString("position");
            int pr = rs.getInt("priority"); 

         if(id==eid)
            {
                if((pos.equals(post))&&(pri==pr))
                {   
                    out.print("<html><head>");
                    out.print("<script type=\"text/javascript\">alert(" + "Details Authenticated" + ");</script>");
                    out.print("</head><body></body></html>");
                }
            }
        }
         rs.close();
         conn.close();
      }