common.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. var SITE_URL = window.location.toString().split('/index.php')[0];
  2. SITE_URL = SITE_URL.replace(/(\/+)$/g, '');
  3. jQuery.extend({
  4. getCookie : function(sName) {
  5. sName = COOKIE_PRE + sName;
  6. var aCookie = document.cookie.split("; ");
  7. for (var i=0; i < aCookie.length; i++){
  8. var aCrumb = aCookie[i].split("=");
  9. if (sName == aCrumb[0]) return decodeURIComponent(aCrumb[1]);
  10. }
  11. return '';
  12. },
  13. setCookie : function(sName, sValue, sExpires) {
  14. sName = COOKIE_PRE + sName;
  15. var sCookie = sName + "=" + encodeURIComponent(sValue);
  16. if (sExpires != null) sCookie += "; expires=" + sExpires;
  17. document.cookie = sCookie;
  18. },
  19. removeCookie : function(sName) {
  20. sName = COOKIE_PRE + sName;
  21. document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
  22. }
  23. });
  24. function drop_confirm(msg, url){
  25. if(confirm(msg)){
  26. window.location = url;
  27. }
  28. }
  29. function go(url){
  30. window.location = url;
  31. }
  32. /* 格式化金额 */
  33. function price_format(price){
  34. if(typeof(PRICE_FORMAT) == 'undefined'){
  35. PRICE_FORMAT = '&yen;%s';
  36. }
  37. price = number_format(price, 2);
  38. return PRICE_FORMAT.replace('%s', price);
  39. }
  40. function number_format(num, ext){
  41. if(ext < 0){
  42. return num;
  43. }
  44. num = Number(num);
  45. if(isNaN(num)){
  46. num = 0;
  47. }
  48. var _str = num.toString();
  49. var _arr = _str.split('.');
  50. var _int = _arr[0];
  51. var _flt = _arr[1];
  52. if(_str.indexOf('.') == -1){
  53. /* 找不到小数点,则添加 */
  54. if(ext == 0){
  55. return _str;
  56. }
  57. var _tmp = '';
  58. for(var i = 0; i < ext; i++){
  59. _tmp += '0';
  60. }
  61. _str = _str + '.' + _tmp;
  62. }else{
  63. if(_flt.length == ext){
  64. return _str;
  65. }
  66. /* 找得到小数点,则截取 */
  67. if(_flt.length > ext){
  68. _str = _str.substr(0, _str.length - (_flt.length - ext));
  69. if(ext == 0){
  70. _str = _int;
  71. }
  72. }else{
  73. for(var i = 0; i < ext - _flt.length; i++){
  74. _str += '0';
  75. }
  76. }
  77. }
  78. return _str;
  79. }
  80. /* 火狐下取本地全路径 */
  81. function getFullPath(obj)
  82. {
  83. if(obj)
  84. {
  85. //ie
  86. if (window.navigator.userAgent.indexOf("MSIE")>=1)
  87. {
  88. obj.select();
  89. if(window.navigator.userAgent.indexOf("MSIE") == 25){
  90. obj.blur();
  91. }
  92. return document.selection.createRange().text;
  93. }
  94. //firefox
  95. else if(window.navigator.userAgent.indexOf("Firefox")>=1)
  96. {
  97. if(obj.files)
  98. {
  99. //return obj.files.item(0).getAsDataURL();
  100. return window.URL.createObjectURL(obj.files.item(0));
  101. }
  102. return obj.value;
  103. }
  104. return obj.value;
  105. }
  106. }
  107. /* 转化JS跳转中的 & */
  108. function transform_char(str)
  109. {
  110. if(str.indexOf('&'))
  111. {
  112. str = str.replace(/&/g, "%26");
  113. }
  114. return str;
  115. }
  116. //图片比例缩放控制
  117. function DrawImage(ImgD,FitWidth,FitHeight){
  118. var image=new Image();
  119. image.src=ImgD.src;
  120. if(image.width>0 && image.height>0)
  121. {
  122. if(image.width/image.height>= FitWidth/FitHeight)
  123. {
  124. if(image.width>FitWidth)
  125. {
  126. ImgD.width=FitWidth;
  127. ImgD.height=(image.height*FitWidth)/image.width;
  128. }
  129. else
  130. {
  131. ImgD.width=image.width;
  132. ImgD.height=image.height;
  133. }
  134. }
  135. else
  136. {
  137. if(image.height>FitHeight)
  138. {
  139. ImgD.height=FitHeight;
  140. ImgD.width=(image.width*FitHeight)/image.height;
  141. }
  142. else
  143. {
  144. ImgD.width=image.width;
  145. ImgD.height=image.height;
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * 浮动DIV定时显示提示信息,如操作成功, 失败等
  152. * @param string tips (提示的内容)
  153. * @param int height 显示的信息距离浏览器顶部的高度
  154. * @param int time 显示的时间(按秒算), time > 0
  155. * @sample <a href="javascript:void(0);" onclick="showTips( '操作成功', 100, 3 );">点击</a>
  156. * @sample 上面代码表示点击后显示操作成功3秒钟, 距离顶部100px
  157. * @copyright ZhouHr 2010-08-27
  158. */
  159. function showTips( tips, height, time ){
  160. var windowWidth = document.documentElement.clientWidth;
  161. var tipsDiv = '<div class="tipsClass">' + tips + '</div>';
  162. $( 'body' ).append( tipsDiv );
  163. $( 'div.tipsClass' ).css({
  164. 'top' : 200 + 'px',
  165. 'left' : ( windowWidth / 2 ) - ( tips.length * 13 / 2 ) + 'px',
  166. 'position' : 'fixed',
  167. 'padding' : '20px 50px',
  168. 'background': '#EAF2FB',
  169. 'font-size' : 14 + 'px',
  170. 'margin' : '0 auto',
  171. 'text-align': 'center',
  172. 'width' : 'auto',
  173. 'color' : '#333',
  174. 'border' : 'solid 1px #A8CAED',
  175. 'opacity' : '0.90',
  176. 'z-index' : '9999'
  177. }).show();
  178. setTimeout( function(){$( 'div.tipsClass' ).fadeOut().remove();}, ( time * 1000 ) );
  179. }
  180. function trim(str) {
  181. return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
  182. }
  183. //弹出框登录
  184. function login_dialog(){
  185. CUR_DIALOG = ajax_form('login','登录','index.php?act=login&inajax=1',360,1)
  186. }
  187. /* 显示Ajax表单 */
  188. function ajax_form(id, title, url, width, model)
  189. {
  190. if (!width) width = 480;
  191. if (!model) model = 1;
  192. var d = DialogManager.create(id);
  193. d.setTitle(title);
  194. d.setContents('ajax', url);
  195. d.setWidth(width);
  196. d.show('center',model);
  197. return d;
  198. }
  199. function ajax_notice(id, title, url, width, model) {
  200. if (!width) width = 480;
  201. if (!model) model = 0;
  202. var d = DialogManager.create(id);
  203. d.setTitle(title);
  204. d.setContents('ajax_notice', url);
  205. d.setWidth(width);
  206. d.show('center',model);
  207. return d;
  208. }
  209. //显示一个正在等待的消息
  210. function loading_form(id, title, _text, width, model) {
  211. if (!width) width = 480;
  212. if (!model) model = 0;
  213. var d = DialogManager.create(id);
  214. d.setTitle(title);
  215. d.setContents('loading', { text: _text });
  216. d.setWidth(width);
  217. d.show('center',model);
  218. return d;
  219. }
  220. //显示一个提示消息
  221. function message_notice(id, title, _text, width, model) {
  222. if (!width) width = 480;
  223. if (!model) model = 0;
  224. var d = DialogManager.create(id);
  225. d.setTitle(title);
  226. d.setContents('message', { type: 'notice', text: _text });
  227. d.setWidth(width);
  228. d.show('center',model);
  229. return d;
  230. }
  231. //显示一个带确定、取消按钮的消息
  232. function message_confirm(id, title, _text, width, model) {
  233. if (!width) width = 480;
  234. if (!model) model = 0;
  235. var d = DialogManager.create(id);
  236. d.setTitle(title);
  237. d.setContents('message', { type: 'confirm', text: _text });
  238. d.setWidth(width);
  239. d.show('center',model);
  240. return d;
  241. }
  242. //显示一个内容为自定义HTML内容的消息
  243. function html_form(id, title, _html, width, model) {
  244. if (!width) width = 480;
  245. if (!model) model = 0;
  246. var d = DialogManager.create(id);
  247. d.setTitle(title);
  248. d.setContents(_html);
  249. d.setWidth(width);
  250. d.show('center',0);
  251. return d;
  252. }
  253. //显示一个消息 消息的内容为IFRAME方式
  254. function iframe_form(id, title, _url, width, height,fresh) {
  255. if (!width) width = 480;
  256. var rnd=Math.random();
  257. rnd=Math.floor(rnd*10000);
  258. var d = DialogManager.create(id);
  259. d.setTitle(title);
  260. var _html = "<iframe id='iframe_"+rnd+"' src='" + _url + "' width='" + width + "' height='" + height + "' frameborder='0'></iframe>";
  261. d.setContents(_html);
  262. d.setWidth(width + 20);
  263. d.setHeight(height + 60);
  264. d.show('center');
  265. $("#iframe_"+rnd).attr("src",_url);
  266. return d;
  267. }
  268. //收藏店铺js
  269. function collect_store(fav_id,jstype,jsobj){
  270. $.get('index.php?act=index&op=login', function(result){
  271. if(result=='0'){
  272. login_dialog();
  273. }else{
  274. var url = 'index.php?act=member_favorites&op=favoritesstore';
  275. $.getJSON(url, {'fid':fav_id}, function(data){
  276. if (data.done)
  277. {
  278. showDialog(data.msg, 'succ','','','','','','','','',2);
  279. if(jstype == 'count'){
  280. $('[nctype="'+jsobj+'"]').each(function(){
  281. $(this).html(parseInt($(this).text())+1);
  282. });
  283. }
  284. if(jstype == 'succ'){
  285. $('[nctype="'+jsobj+'"]').each(function(){
  286. $(this).html("收藏成功");
  287. });
  288. }
  289. if(jstype == 'store'){
  290. $('[nc_store="'+fav_id+'"]').each(function(){
  291. $(this).before('<span class="goods-favorite" title="该店铺已收藏"><i class="have">&nbsp;</i></span>');
  292. $(this).remove();
  293. });
  294. }
  295. }
  296. else
  297. {
  298. showDialog(data.msg, 'notice');
  299. }
  300. });
  301. }
  302. });
  303. }
  304. //收藏商品js
  305. function collect_goods(fav_id,jstype,jsobj){
  306. $.get('index.php?act=index&op=login', function(result){
  307. if(result=='0'){
  308. login_dialog();
  309. }else{
  310. var url = 'index.php?act=member_favorites&op=favoritesgoods';
  311. $.getJSON(url, {'fid':fav_id}, function(data){
  312. if (data.done)
  313. {
  314. showDialog(data.msg, 'succ','','','','','','','','',2);
  315. if(jstype == 'count'){
  316. $('[nctype="'+jsobj+'"]').each(function(){
  317. $(this).html(parseInt($(this).text())+1);
  318. });
  319. }
  320. if(jstype == 'succ'){
  321. $('[nctype="'+jsobj+'"]').each(function(){
  322. $(this).html("收藏成功");
  323. });
  324. }
  325. }
  326. else
  327. {
  328. showDialog(data.msg, 'notice');
  329. }
  330. });
  331. }
  332. });
  333. }
  334. //取得COOKIE值
  335. //function getcookie(name){
  336. // return $.cookie(COOKIE_PRE+name);
  337. //}
  338. //动态加载js,css
  339. //$.include(['http://www.shopnc.net/script/a.js','/css/css.css']);
  340. $.extend({
  341. include: function(file)
  342. {
  343. var files = typeof file == "string" ? [file] : file;
  344. for (var i = 0; i < files.length; i++)
  345. {
  346. var name = files[i].replace(/^\s|\s$/g, "");
  347. var att = name.split('.');
  348. var ext = att[att.length - 1].toLowerCase();
  349. var isCSS = ext == "css";
  350. var tag = isCSS ? "link" : "script";
  351. var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
  352. var link = (isCSS ? "href" : "src") + "='" + SITEURL+'/' + name + "'";
  353. if ($(tag + "[" + link + "]").length == 0) $('body').append("<" + tag + attr + link + "></" + tag + ">");
  354. }
  355. }
  356. });
  357. $(function(){
  358. if(typeof(SITEURL) == 'string') SITE_URL = SITEURL;//重写SITE_URL
  359. //首页左侧分类菜单
  360. $("#category ul").find("li").each(
  361. function() {
  362. $(this).mouseover(
  363. function() {
  364. menu = $("#" + this.id + "_menu");
  365. menu_height = menu.height();
  366. if (menu_height < 40) menu.height(60);
  367. menu_height = menu.height();
  368. li_top = $(this).position().top;
  369. if ((li_top > 40) && (menu_height >= li_top)) $(menu).css("top",-li_top+20);
  370. if ((li_top > 160) && (menu_height >= li_top)) $(menu).css("top",-li_top+40);
  371. if ((li_top > 240) && (li_top > menu_height)) $(menu).css("top",menu_height-li_top);
  372. if (li_top > 360) $(menu).css("top",60-menu_height);
  373. if ((li_top > 40) && (menu_height <= 90)) $(menu).css("top",-20);
  374. menu.show();
  375. $(this).addClass("a");
  376. }
  377. );
  378. $(this).mouseout(
  379. function() {
  380. $(this).removeClass("a");
  381. $("#" + this.id + "_menu").hide();
  382. }
  383. );
  384. }
  385. );
  386. });