经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
前端之本地存储和jqueryUI
来源:cnblogs  作者:YifChan  时间:2019/6/17 8:59:59  对本文有异议

本地存储

本地存储分为cookie,以及新增的localStorage和sessionStorage

1、cookie 存储在本地,容量最大4k,在同源的http请求时携带传递,损耗带宽,可设置访问路径,只有此路径及此路径的子路径才能访问此cookie,在设置的过期时间之前有效。
jquery 设置cookie

  1. $.cookie('mycookie','123',{expires:7,path:'/'});
  2. jquery 获取cookie
  3. $.cookie('mycookie');

 

2、localStorage 存储在本地,容量为5M或者更大,不会在请求时候携带传递,在所有同源窗口中共享,数据一直有效,除非人为删除,可作为长期数据。

  1. //设置:
  2. localStorage.setItem("dat", "456");
  3. localStorage.dat = '456';
  4. //获取:
  5. localStorage.getItem("dat");
  6. localStorage.dat
  7. //删除
  8. localStorage.removeItem("dat");

 

3、sessionStorage 存储在本地,容量为5M或者更大,不会在请求时候携带传递,在同源的当前窗口关闭前有效。

localStorage 和 sessionStorage 合称为Web Storage , Web Storage支持事件通知机制,可以将数据更新的通知监听者,Web Storage的api接口使用更方便。

iPhone的无痕浏览不支持Web Storage,只能用cookie。

注意,只有cookie需要使用cookie插件,webstorage不需要使用;


设置cookie插件:jquery.cookie.js

  1. /*!
  2. * jQuery Cookie Plugin v1.4.1
  3. * https://github.com/carhartl/jquery-cookie
  4. *
  5. * Copyright 2013 Klaus Hartl
  6. * Released under the MIT license
  7. */
  8. (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. // AMD
  11. define(['jquery'], factory);
  12. } else if (typeof exports === 'object') {
  13. // CommonJS
  14. factory(require('jquery'));
  15. } else {
  16. // Browser globals
  17. factory(jQuery);
  18. }
  19. }(function ($) {
  20. var pluses = /\+/g;
  21. function encode(s) {
  22. return config.raw ? s : encodeURIComponent(s);
  23. }
  24. function decode(s) {
  25. return config.raw ? s : decodeURIComponent(s);
  26. }
  27. function stringifyCookieValue(value) {
  28. return encode(config.json ? JSON.stringify(value) : String(value));
  29. }
  30. function parseCookieValue(s) {
  31. if (s.indexOf('"') === 0) {
  32. // This is a quoted cookie as according to RFC2068, unescape...
  33. s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  34. }
  35. try {
  36. // Replace server-side written pluses with spaces.
  37. // If we can't decode the cookie, ignore it, it's unusable.
  38. // If we can't parse the cookie, ignore it, it's unusable.
  39. s = decodeURIComponent(s.replace(pluses, ' '));
  40. return config.json ? JSON.parse(s) : s;
  41. } catch (e) {
  42. }
  43. }
  44. function read(s, converter) {
  45. var value = config.raw ? s : parseCookieValue(s);
  46. return $.isFunction(converter) ? converter(value) : value;
  47. }
  48. var config = $.cookie = function (key, value, options) {
  49. // Write
  50.  
  51. if (value !== undefined && !$.isFunction(value)) {
  52. options = $.extend({}, config.defaults, options);
  53. if (typeof options.expires === 'number') {
  54. var days = options.expires, t = options.expires = new Date();
  55. t.setTime(+t + days * 864e+5);
  56. }
  57. return (document.cookie = [
  58. encode(key), '=', stringifyCookieValue(value),
  59. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  60. options.path ? '; path=' + options.path : '',
  61. options.domain ? '; domain=' + options.domain : '',
  62. options.secure ? '; secure' : ''
  63. ].join(''));
  64. }
  65. // Read
  66.  
  67. var result = key ? undefined : {};
  68. // To prevent the for loop in the first place assign an empty array
  69. // in case there are no cookies at all. Also prevents odd result when
  70. // calling $.cookie().
  71. var cookies = document.cookie ? document.cookie.split('; ') : [];
  72. for (var i = 0, l = cookies.length; i < l; i++) {
  73. var parts = cookies[i].split('=');
  74. var name = decode(parts.shift());
  75. var cookie = parts.join('=');
  76. if (key && key === name) {
  77. // If second argument (value) is a function it's a converter...
  78. result = read(cookie, value);
  79. break;
  80. }
  81. // Prevent storing a cookie that we couldn't decode.
  82. if (!key && (cookie = read(cookie)) !== undefined) {
  83. result[name] = cookie;
  84. }
  85. }
  86. return result;
  87. };
  88. config.defaults = {};
  89. $.removeCookie = function (key, options) {
  90. if ($.cookie(key) === undefined) {
  91. return false;
  92. }
  93. // Must not alter options, thus extending a fresh object...
  94. $.cookie(key, '', $.extend({}, options, {expires: -1}));
  95. return !$.cookie(key);
  96. };
  97. }));
jquery.cookie.js

 

cookie简单使用示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript" src="js/jquery.cookie.js"></script>
  8. <script type="text/javascript">
  9.  
  10. // 设置cookie 过期时间为7天,存在网站根目录下
  11. //$.cookie('mycookie','ok!',{expires:7,path:'/'});
  12.  
  13. //读取cookie
  14. var mycookie = $.cookie('mycookie');
  15. alert(mycookie);
  16. </script>
  17. </head>
  18. <body>
  19. <h1>测试页面</h1>
  20. </body>
  21. </html>
cookie简单使用示例

webstorage的使用示例-localStorage/sessionStorage 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript">
  7.  
  8. //localStorage.setItem("dat", "456");
  9.  
  10. //sessionStorage.setItem('dat1','789');
  11.  
  12. </script>
  13. </head>
  14. <body>
  15. <h1>测试webstorage</h1>
  16. </body>
  17. </html>
webstorage的使用示例

cookie练习-只弹一次的弹框

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript" src="js/jquery.cookie.js"></script>
  8. <script type="text/javascript">
  9. $(function () {
  10. var hasread = $.cookie('hasread');
  11. //alert(hasread);
  12.  
  13. // 判断是否存了cookie,没有就弹出弹框
  14. if (hasread == undefined) {
  15. $('.pop_con').show();
  16. }
  17. //用户点击知道后,存cookie,把弹框关掉
  18. $('#user_read').click(function () {
  19. $.cookie('hasread', 'read', {expires: 7, path: '/'});
  20. $('.pop_con').hide();
  21. })
  22. })
  23. </script>
  24. <script type="text/javascript"></script>
  25. <style type="text/css">
  26. .pop_con {
  27. display: none;
  28. }
  29. .pop {
  30. position: fixed;
  31. width: 500px;
  32. height: 300px;
  33. background-color: #fff;
  34. border: 3px solid #000;
  35. left: 50%;
  36. top: 50%;
  37. margin-left: -250px;
  38. margin-top: -150px;
  39. z-index: 9999;
  40. }
  41. .mask {
  42. position: fixed;
  43. width: 100%;
  44. height: 100%;
  45. background-color: #000;
  46. opacity: 0.3;
  47. filter: alpha(opacity=30);
  48. left: 0;
  49. top: 0;
  50. z-index: 9990;
  51.  
  52. }
  53. .close {
  54. float: right;
  55. font-size: 30px;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60.  
  61. <div class="pop_con">
  62. <div class="pop">
  63. 亲!本网站最近有优惠活动!请强力关注!
  64. <a href="#" id="close" class="close">×</a>
  65.  
  66. <a href="javascript:;" id="user_read">朕知道了!</a>
  67. </div>
  68. <div class="mask"></div>
  69. </div>
  70.  
  71. <h1>网站内容</h1>
  72. </body>
  73. </html>
cookie练习-只弹一次的弹框

 

 

jqueryUI

jQuery UI是以 jQuery 为基础的代码库。包含底层用户交互、动画、特效和可更换主题的可视控件。我们可以直接用它来构建具有很好交互性的web应用程序。

jqueryUI 网址
http://jqueryui.com/

常用jqueryUI插件:

Draggable
1、设置数值的滑动条
2、自定义滚动条

 

拖拽滑动条设置数值示例(类似于游戏中中设置灵敏度的设置条)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>drag</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript" src="js/jquery-ui.min.js"></script>
  8. <script type="text/javascript">
  9. $(function () {
  10. $('.dragbar').draggable({
  11. axis: 'x',
  12. containment: 'parent',
  13. //containment:[0,0,600,0]
  14.  
  15. //设置拖动时候的透明度
  16. opacity: 0.6,
  17. drag: function (ev, ui) {
  18. //console.log(ui.position.left);
  19.  
  20. //获取拖动的距离
  21. var nowleft = ui.position.left;
  22. $('.progress').css({width: nowleft});
  23. $('.slide_count').val(parseInt(nowleft * 100 / 570));
  24. }
  25. });
  26. })
  27. </script>
  28. <style type="text/css">
  29. .slidebar_con {
  30. width: 650px;
  31. height: 32px;
  32. margin: 50px auto 0;
  33. }
  34. .slidebar {
  35. width: 600px;
  36. height: 30px;
  37. border: 1px solid #ccc;
  38. float: left;
  39. position: relative;
  40. }
  41. .slidebar .dragbar {
  42. width: 30px;
  43. height: 30px;
  44. background-color: gold;
  45. cursor: pointer;
  46. position: absolute;
  47. left: 0;
  48. top: 0;
  49. }
  50. .slidebar .progress {
  51. width: 0px;
  52. height: 30px;
  53. background-color: #f0f0f0;
  54. position: absolute;
  55. left: 0;
  56. top: 0;
  57. }
  58. .slide_count {
  59. padding: 0;
  60. float: right;
  61. width: 40px;
  62. height: 30px;
  63. border: 1px solid #ccc;
  64. text-align: center;
  65. font-size: 16px;
  66. }
  67.  
  68. </style>
  69. </head>
  70. <body>
  71. <div class="slidebar_con">
  72. <div class="slidebar">
  73. <div class="progress"></div>
  74. <div class="dragbar"></div>
  75. </div>
  76. <input type="text" name="" value="0" class="slide_count">
  77. </div>
  78. </body>
  79. </html>
拖拽滑动条示例

网页自定义页面滚动条示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>自定义滚动条</title>
  6. <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
  7. <script type="text/javascript" src="js/jquery-ui.min.js"></script>
  8. <script type="text/javascript">
  9. $(function () {
  10. var h = $('.paragraph').outerHeight();
  11. //整体文本的高度减去外面容器的高度
  12. var hide = h - 500;
  13. $('.scroll_bar').draggable({
  14. axis: 'y',
  15. containment: 'parent',
  16. opacity: 0.6,
  17. drag: function (ev, ui) {
  18. var nowtop = ui.position.top;
  19. var nowscroll = parseInt(nowtop / 290 * hide);
  20. $('.paragraph').css({top: -nowscroll});
  21. }
  22. });
  23. })
  24. </script>
  25. <style type="text/css">
  26. .scroll_con {
  27. width: 400px;
  28. height: 500px;
  29. border: 1px solid #ccc;
  30. margin: 50px auto 0;
  31. position: relative;
  32. overflow: hidden;
  33. }
  34. .paragraph {
  35. width: 360px;
  36. position: absolute;
  37. left: 0;
  38. top: 0;
  39. padding: 10px 20px;
  40. font-size: 14px;
  41. font-family: 'Microsoft Yahei';
  42. line-height: 32px;
  43. text-indent: 2em;
  44. }
  45. .scroll_bar_con {
  46. width: 10px;
  47. height: 490px;
  48. position: absolute;
  49. right: 5px;
  50. top: 5px;
  51. }
  52. .scroll_bar {
  53. width: 10px;
  54. height: 200px;
  55. background-color: #ccc;
  56. position: absolute;
  57. left: 0;
  58. top: 0;
  59. cursor: pointer;
  60. border-radius: 5px;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div class="scroll_con">
  66. <div class="paragraph">
  67. 2005年以后,互联网进入Web2.0时代,各种类似桌面软件的Web应用大量涌现,网站的前端由此发生了翻天覆地的变化。网页不再只是承载单一的文字和图片,各种富媒体让网页的内容更加生动,网页上软件化的交互形式为用户提供了更好的使用体验,这些都是基于前端技术实现的。以前会Photoshop和Dreamweaver就可以制作网页,现在只掌握这些已经远远不够了。无论是开发难度上,还是开发方式上,现在的网页制作都更接近传统的网站后台开发,所以现在不再叫网页制作,而是叫Web前端开发。Web前端开发在产品开发环节中的作用变得越来越重要,而且需要专业的前端工程师才能做好,这方面的专业人才近几年来备受青睐。Web前端开发是一项很特殊的工作,涵盖的知识面非常广,既有具体的技术,又有抽象的理念。简单地说,它的主要职能就是把网站的界面更好地呈现给用户。
  68. 掌握HTML是网页的核心,是一种制作万维网页面的标准语言,是万维网浏览器使用的一种语言,它消除了不同计算机之间信息交流的障碍。因此,它是目前网络上应用最为广泛的语言,也是构成网页文档的主要语言,学好HTML是成为Web开发人员的基本条件。
  69. 学好CSS是网页外观的重要一点,CSS可以帮助把网页外观做得更加美观。
  70. 学习JavaScript的基本语法,以及如何使用JavaScript编程将会提高开发人员的个人技能。
  71. 了解Unix和Linux的基本知识虽然这两点很基础,但是开发人员了解Unix和Linux的基本知识是有益无害的。
  72. 了解Web服务器当你对Apache的基本配置,htaccess配置技巧有一些掌握的话,将来必定受益,而且这方面的知识学起来也相对容易。
  73. </div>
  74. <div class="scroll_bar_con">
  75. <div class="scroll_bar">
  76. </div>
  77. </div>
  78. </div>
  79. </body>
  80. </html>
网页自定义页面滚动条示例

 

原文链接:http://www.cnblogs.com/yifchan/p/html-1-12.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号