经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jQuery实现简单的图片淡出淡出效果
来源:cnblogs  作者:日天达人  时间:2019/4/10 8:52:56  对本文有异议

整体思路:

1.实现页面布局,设置css样式

2.用jQuery获取需要用到的变量

3.用jQuery为两个按钮绑定事件

一.页面布局:

 

  1. <div class="d1">
  2. //随便在网上找一张图片放入img中//
  3. <img src="https://dummyimage.com/900x400/0000ff/ff" alt="" class="c1 c2">
  4. <div class="d2">
  5. <input type="button" value="<-" id="b1">
  6. <input type="button" value="->" id="b2">
  7. </div>
  8. </div>
  1. <style>
  2. body{
  3. margin: 0 0 0 0;
  4. height: 1000px;
  5. width: 100%;
  6. }
  7. .d1{
  8. position: absolute;
  9. width: 100%;
  10. height: 500px;
  11. top: 50%;
  12. margin-top: -250px;
  13. }
  14. .d2{
  15. margin-left: 950px;
  16. }
  17. .d1 img{
  18. margin-left: 50px;
  19. position: relative;
  20. }
  21. .c1{
  22. display: block;
  23. padding-left: 500px;
  24. }
  25. </style>
css布局

我的css布局仅仅做了居中,各位可以做的更加美观性

 

二.jQuery获取需要用到的变量

 

  1. //imgList中放入你要加入的图片,记得要加入在div中定义的起始图片//
  2. var imgList=['https://dummyimage.com/900x400/0000ff/ff','https://dummyimage.com/900x400/00/ff','https://dummyimage.com/900x400/ff0000/ff'];
  3. var $imgEle=$('img');//获取div中的img
  4. var nowSrc=imgList.indexOf($imgEle[0].src);//获取起始图片的索引值,后面函数要用到
  5. //获取两个按钮
  6. var $b1Ele=$('#b1');
  7. var $b2Ele=$('#b2');

三.用jQuery为两个按钮绑定事件

首先写$b1El1的函数:

 

  1. function f1(){
  2. //先让当前图片淡出,时间为0.5毫秒
  3. $imgEle.fadeOut(500);
  4. //进行判断,如果索引值为0,让索引变成列表的最大值
  5. if(nowSrc===0){
  6. nowSrc=imgList.length-1;
  7. }
  8. //索引值不为0,进行--
  9. else {
  10. nowSrc--;
  11. }
  12. //因为我淡出的时间设置为0.5毫秒,所以我设置计时器,让下面的代码0.5毫秒后启动
  13. t=setTimeout(function () {
  14. //更换图片的src
  15. $imgEle.attr('src',imgList[nowSrc]);
  16. //图片淡入,时间为0.5毫秒
  17. $imgEle.fadeIn(500);
  18. },500);
  19. }

 

为$b1El1绑定函数:

  1. $b1Ele.on('click',f1);

同理可以写出按钮2的函数,并进行绑定

  1. function f2(){
  2. $imgEle.fadeOut(500);
  3. console.log(nowSrc);
  4. if(nowSrc===imgList.length-1){
  5. nowSrc=0;
  6. }
  7. else {
  8. nowSrc++;
  9. }
  10. t2=setTimeout(function () {
  11. $imgEle.attr('src',imgList[nowSrc]);
  12. $imgEle.fadeIn(500);
  13. },500);
  14. t2=null
  15. }
  16. $b2Ele.on('click',f2);

下面是整体代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <!--设置css样式-->
  7. <style>
  8. body{
  9. margin: 0 0 0 0;
  10. height: 1000px;
  11. width: 100%;
  12. }
  13. .d1{
  14. position: absolute;
  15. width: 100%;
  16. height: 500px;
  17. top: 50%;
  18. margin-top: -250px;
  19. }
  20. .d2{
  21. margin-left: 950px;
  22. }
  23. .d1 img{
  24. margin-left: 50px;
  25. position: relative;
  26. }
  27. .c1{
  28. display: block;
  29. padding-left: 500px;
  30. }
  31. </style>
  32.  
  33. <script src="jQuery.js"></script>
  34. </head>
  35. <body>
  36. <div class="d1">
  37. <img src="https://dummyimage.com/900x400/0000ff/ff" alt="" class="c1 c2">
  38. <div class="d2">
  39. <input type="button" value="<-" id="b1">
  40. <input type="button" value="->" id="b2">
  41. </div>
  42. </div>
  43. <script>
  44. var imgList=['https://dummyimage.com/900x400/0000ff/ff','https://dummyimage.com/900x400/00/ff','https://dummyimage.com/900x400/ff0000/ff'];
  45. var $imgEle=$('img');
  46. var nowSrc=imgList.indexOf($imgEle[0].src);
  47. var $b1Ele=$('#b1');
  48. var $b2Ele=$('#b2');
  49. function f1(){
  50. $imgEle.fadeOut(500);
  51. console.log(nowSrc);
  52. if(nowSrc===0){
  53. nowSrc=imgList.length-1;
  54. }
  55. else {
  56. nowSrc--;
  57. }
  58. t=setTimeout(function () {
  59. $imgEle.attr('src',imgList[nowSrc]);
  60. $imgEle.fadeIn(500);
  61. },500);
  62. }
  63. function f2(){
  64. $imgEle.fadeOut(500);
  65. console.log(nowSrc);
  66. if(nowSrc===imgList.length-1){
  67. nowSrc=0;
  68. }
  69. else {
  70. nowSrc++;
  71. }
  72. t2=setTimeout(function () {
  73. $imgEle.attr('src',imgList[nowSrc]);
  74. $imgEle.fadeIn(500);
  75. },500);
  76. t2=null
  77. }
  78. $b1Ele.on('click',f1);
  79. $b2Ele.on('click',f2);
  80. </script>
  81. </body>
  82. </html>
全部代码

 

原文链接:http://www.cnblogs.com/98WDJ/p/10679228.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号