<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").css({"color": "red", "font-size": "20px", "text-align": "center"}); }); </script> </head> <body> <p>Hello, jQuery!</p> </body> </html>
<p>
elements and pass .css()
Methods change their style. <p>2. Add animation effects<p>In addition to changing the static style, we can also use jQuery to add animation effects to add vitality to the web page. Here is a simple animation effect example: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#btn").click(function(){ $("#box").animate({left: '250px'}); }); }); </script> </head> <body> <button id="btn">点击移动盒子</button> <div id="box" style="width: 100px; height: 100px; background-color: red; position: relative;"></div> </body> </html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#hover").hover(function(){ $(this).css("background-color", "yellow"); }, function(){ $(this).css("background-color", "white"); }); }); </script> </head> <body> <div id="hover" style="width: 200px; height: 100px; background-color: white;">悬停在我上面</div> </body> </html>
#hover
element, the background color changes to yellow, and back to white when the mouse is removed. .
<p>Summary:
<p>Through the above example, we can see that using jQuery can easily customize the web page style. Whether it's changing text styles, adding animation effects, or responding to user interaction, it can all be achieved with simple jQuery code. I hope the above examples can help you better customize your web page styles and provide users with a better experience. The above is the detailed content of Use jQuery to easily customize web page styles. For more information, please follow other related articles on the PHP Chinese website!