網站首頁 編程語言 正文
本文實例為大家分享了jquery實現頁面彈球效果的具體代碼,供大家參考,具體內容如下
像windows屏保一樣,實現小球在頁面中的彈跳,并且隨著頁面的改變而改變
如下圖:
源碼
<!doctype html> <html><head> <meta charset="utf-8"> <title>無標題文檔</title> </head> ? <style type="text/css"> ? ?*{ ?? ? ? margin:0px; ?? ? ? padding:0px;} ? ?#container{ ?? ? ? width:800px; ?? ? ? height:500px; ?? ? ? background:#FFF; ?? ? ? margin:0px auto; ?? ? ? margin-top:100px;} ? ?#b1{ ?? ? ? width:50px; ?? ? ? height:50px; ?? ? ? background-color:#FFCCCC; ?? ? ? border-radius:100%; ?? ? ? position:fixed; ?? ? ? ? ?} ? ?#b2{ ?? ? ? width:80px; ?? ? ? height:80px; ?? ? ? background-color:#9EC0C9; ?? ? ? border-radius:100%; ?? ? ? position:fixed; ?? ? ? ? ?} ? ? #b3{ ? ? ? ?width:60px; ? ? ? ?height:60px; ? ? ? ?background-color:#336633; ? ? ? ?border-radius:100%; ? ? ? ?position:fixed; ? ? ? ? ? } </style> ? <script src="jquery-3.3.1.js"></script> <script type="text/javascript"> ? ? ?? ? ? //調用移動浮窗方法并按順序傳入正確參數["obj",x,y,l,t,m],obj必填 ?? ?/* ?? ? ? move_w:能夠移動的寬度 ?? ? ? move_h:能夠移動的高度 ?? ? ? x:左右移動速度 ?? ? ? y:上下移動速度 ?? ? ? l:初始left的值 ?? ? ? t:初始top的值 ?? ? ? m:時間 ?? ? ?? ?? ?*/ ? ? function move_obj(obj, x, y, l, t, m) { ? ? if (obj == undefined) { ? ? ? ? alert("方法調用錯誤,請傳入正確參數!"); ? ? ? ? return; ? ? } ?? ? ? ? //當前瀏覽器窗口大小 ? ? move_w = $(window).width() ; ? ? move_h = $(window).height() ; ? ? ? ? ? ? //若瀏覽器窗口大小改變 ? ? $(window).resize(function() {? ? ? ? ? ?move_w = $(window).width() ; ? ? ? ? ?move_h = $(window).height() ; ? ? }); ? ? ? //移動的速度和初始位置 ? ? x = (x == undefined || x == '') ? 3 : x; ? ? y = (y == undefined || y == '') ? 3 : y; ? ? l = (l == undefined || l == '') ? 0 : l; ? ? t = (t == undefined || t == '') ? 0 : t; ? ? m = (m == undefined || m == '') ? 80 : m; ?? ? ? ? //移動 ? ? function moving() { ? ? ? ? ? if( l >= move_w - $(obj).width() || l < 0 ){ ?//如果碰到瀏覽器左右壁 ? ? ? ? ? ? x = -x; ? ? ? ? } ? ? ? ? ? if(t >= move_h - $(obj).height() || t < 0){ ?//如果碰到瀏覽器上下壁 ? ? ? ? ? ? y = -y; ? ? ? ? } ? ? ? ? ? ? ? ? l += x; ? ? ? ? t += y; ? ? ? ? ? $(obj).css({ ?//改變div的位置 ? ? ? ? ? ? left: l, ? ? ? ? ? ? top: t ? ? ? ? }); ? ? } ?? ? ? ? var timer_move = setInterval(function() { ? ? ? ? moving(); ? ? }, ? ? m); ?? ? ? ? $(obj).mouseover(function() { ? ? ? ? clearInterval(timer_move); ? ? }); ? ? ? $(obj).mouseout(function() { ? ? ? ? timer_move = setInterval(function() { ? ? ? ? ? ? moving(); ? ? ? ? }, ? ? ? ? m); ? ? }); ? } ? ? ?move_obj("#b1",30,10,300,300,100); ? ?move_obj("#b2"); ? ?move_obj("#b3",-20,30,600,500,50); ? </script> ? ? <body bgcolor="#FFFFFF"> ? ? <div id="b1"></div> ? <div id="b2"></div> ? <div id="b3"></div> ? ? </body> </html>
總結
1.$(window).resize()
監測窗口是否改變
2. ?獲取當前瀏覽器窗口大小
move_w = $(window).width() ;
move_h = $(window).height() ;
原文鏈接:https://blog.csdn.net/weixin_40448659/article/details/81124043
相關推薦
- 2022-10-12 Python數據分析之使用scikit-learn構建模型_python
- 2021-12-11 Redis線程模型的原理分析_Redis
- 2022-02-27 springboot引入依賴lombok但是@Data(lombok的一個注解)仍然爆紅
- 2022-04-20 python錯誤提示:Errno?2]?No?such?file?or?directory的解決方法
- 2023-01-08 C#?Winform文本面板帶滾動條的實現過程_C#教程
- 2022-12-11 Rust中Cargo的使用詳解_Rust語言
- 2022-04-02 Docker部署Nginx并配置反向代理_docker
- 2022-06-16 C++與C語言常用的語法對比_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支