首頁 web前端 js教程 js實作彈跳窗外掛程式實例程式碼分享_javascript技巧

js實作彈跳窗外掛程式實例程式碼分享_javascript技巧

May 16, 2016 pm 05:09 PM
彈跳窗插件

目前測試下:支援IE6 火狐 谷歌遊覽器等。

先來看看此元件的基本配置:如下:

複製程式碼 程式碼如下:

this.config = {

 targetCls   :   '.clickElem',   // 點選元素
 title:  '我是龍恩',      //視窗標題
">我是龍

',
 //content            :  'img:http://www.baidu.com/a.jpg',
// 視窗內容{text:具體內容| id :id名| img:圖片連結|
 // iframe:src連結} {string} 
 width:  400,      // ight 。
 showBg      :  true,     // 設置是否顯示遮罩層默認為true 遮罩
 closable    :  '#window-close', // 關閉按鈕
 bgColor     :  '#000',   // 默認顏色
 opacity     : 0.5,// 預設透明度
 position    : {
     x: 0,     :     10000,
 isScroll    : true,      //預設視窗隨著捲動而捲動
 isResize    : true,      // 預設值與視窗縮放>
    };



JS編寫簡單的彈窗插件(含demo和源碼)
2013-12-11 22:30 by 龍恩0707, 409 閱讀, 1 評論, 收藏, 編輯
 最近項目做完了事情不是做完項目很多,今天正好也在調休,所以趁著這個時間研究了一下簡易的JS彈窗功能,當然網上這塊插件非常多,本人也沒有仔細看網上的插件源碼只是憑著日常使用過的彈窗插件有這麼多功能來實現自己的彈窗思路,當然我這個可能實現了基本功能,那麼如果想做的更好更完善需要以後繼續優化!如果有不足之處!請大家多多諒解!
由於彈窗大家都知道是什麼樣的 所以這次沒有做示範效果圖!目前測試下:支援IE6 火狐 谷歌遊覽器等。


先來看看此元件的基本配置:如下:

按 Ctrl C 複製程式碼


按Ctrl C 複製程式碼
1. 支援配置標題內容,標題的高度內容的寬度和高度視窗是否可以拖曳彈窗後是否自動關閉是否顯示遮罩背景顏色及透明度的配置,及視窗的展示位置預設x軸0 y軸0是居中對齊,也可以自己配置x軸  y軸的位置但是要注意是相對於父元素那個X軸y軸如果不指定父元素的相對定位那麼預設情況下會相對於document的。當然視窗內容的寬度和高度不建議超過遊覽器的一屏幕的寬度和高度盡量小於第一屏的寬度和高度因為我以前用別人的彈窗插件會存在點擊關閉按鈕後由於遊覽器有滾動條點擊後觸發滾動條事件導致關閉不了視窗如果內容寬度和高度很大的話也沒有關係視窗自動會出現滾動條的。

2. 視窗的內容配置項目 支援四種 1.text(文字) 可以如下配置 text:

我是龍恩

    2. img(圖) 可依下列設定 img:http://www.baidu.com/a.jpg

    3. id(id節點)  可依下列設定 'id:XX'

    4. iframe   可依下列設定 'iframe:http://www.baidu.com(iframe位址)'

3. 提供彈跳窗後回呼函數: 如彈跳視窗後可以做自己想做的事。

所以彈跳窗組件也沒有什麼好說的 當然如果要用到自己的項目中 css樣式可以自己重新寫的,我JS並沒有寫死掉 只是完成JS基本彈窗業務功能。

HTML程式碼如下:

複製程式碼

程式碼如下:


我是龍恩請點我

我是龍恩請點我

CSS代碼如下

複製代碼 代碼如下:


JS程式碼如下

複製程式碼 程式碼如下:

/**
 * 编写JS弹窗组件
 * @date 2013-12-10
 * @author tugenhua
 * @email 879083421@qq.com
 */

 function Overlay(options){

    this.config = {

 targetCls   :   '.clickElem',   // 点击元素
 title:  '我是龙恩',      // 窗口标题
 content     :  'text:

我是龙恩

',
 //content     :  'img:http://gtms01.alicdn.com/tps/i1/T1USkqFc0dXXb5rXb6-1060-300.jpg',
     // 窗口内容 {text:具体内容 | id:id名 | img:图片链接 |
     // iframe:src链接} {string} 
 width:  400,      // 内容的宽度
 height      :  300,      // 内容的高度
 theight     :  30,// 标题的高度 默认为30
 drag :  true,     // 是否可以拖动 默认为true
 time :  3000,     // 自动关闭窗口的时间 为空或者'undefined'则不关闭
 showBg      :  true,     // 设置是否显示遮罩层 默认为true 遮罩
 closable    :  '#window-close', // 关闭按钮
 bgColor     :  '#000',   // 默认颜色
 opacity     : 0.5,// 默认透明度
 position    : {
     x: 0,
     y: 0   //默认等于0 居中
 },
 zIndex      :     10000,
 isScroll    : true,      //默认情况下 窗口随着滚动而滚动
 isResize    : true,      // 默认情况下 随着窗口缩放而缩放
 callback    : null//弹窗显示后回调函数

    };

    this.cache = {
 isrender     :    true,     // 弹窗html结构只渲染一次
 isshow:    false,    // 窗口是否已经显示出来
 moveable     :    false
    };

    this.init(options);
 }

 Overlay.prototype = {

    constructor: Overlay,

    init: function(options){
 this.config = $.extend(this.config,options || {});
 var self = this,
     _config = self.config,
     _cache = self.cache;
 $(_config.targetCls).each(function(index,item){

     $(item).unbind('click');
     $(item).bind('click',function(){

  // 渲染弹窗HTML结构
  self._renderHTML();

  // 窗口移动
  self._windowMove();
     });
 });

 // 窗口缩放
 self._windowResize('#window-box');

 // 窗口随着滚动条一起滚动
 self._windowIsScroll('#window-box');



    },
    /*
     * 渲染弹窗HTML结构
     */
    _renderHTML: function(){
 var self = this,
     _config = self.config,
     _cache = self.cache;
 var html ='';
 if(_cache.isrender) {

     html+= '

' +
      '';

     $('body').append(html);

     $("#windowbg").css('z-index',_config.zIndex);
     $('#window-content-border').css({'width':_config.width + 'px','height':_config.height + 'px','overflow':'auto'});

     $('.window-title h2').html(_config.title);
     $('.window-title').css({'height':_config.theight + 'px','width':_config.width,'overflow':'hidden'});
     _cache.isrender = false;

     // 判断传递进来的内容格式
     self._contentType();
     if(_config.showBg) {
  // 渲染背景宽度和高度
  self._renderDocHeight();
  $("#windowbg").show();

  self.show();
     }else {
  $("#windowbg").hide();
  self.show();
     }
     self._showDialogPosition("#window-box");
  }else {

     // 如果弹窗已经创建出来的话, 只是隐藏掉了, 那么我们显示出来
     self.show();
     $("#windowbg").animate({"opacity":_config.opacity},'normal');
     if(_config.showBg) {
  $("#windowbg").show();
     }

     self._showDialogPosition("#window-box");
  }
  $(_config.closable).unbind('click');
  $(_config.closable).bind('click',function(){
     // 点击关闭按钮
     self._closed();
  });

  // 渲染后 回调函数
  _config.callback && $.isFunction(_config.callback) && _config.callback();
    },
    /**
     * 显示弹窗
     */
     show: function(){
 var self = this,
     _config = self.config,
     _cache = self.cache;
 $("#window-box") && $("#window-box").show();
 _cache.isshow = true;
 if(_config.time == '' || typeof _config.time == 'undefined') {
     return;
 }else {
     t && clearTimeout(t);
 var t = setTimeout(function(){
  self._closed();
     },_config.time);
 }
     },
     /**
      * 隐藏弹窗
      */
     hide: function(){
 var self = this,
     _cache = self.cache;
 $("#window-box") && $("#window-box").hide();
 _cache.isshow = false;
     },
     /**
      *    判断传进来的内容类型
      */
     _contentType: function(){
 var self = this,
     _config = self.config,
     _cache = self.cache;

 var contentType =  _config.content.substring(0,_config.content.indexOf(":")),
     content = _config.content.substring(_config.content.indexOf(":")+1,_config.content.length);

 switch(contentType) {
     case 'text':
  $('#window-content').html(content);

     break;

     case 'id':
  $('#window-content').html($('#'+content).html());

     break;

     case 'img':
  $('#window-content').html("");

     break;

     case 'iframe':
  $('#window-content').html('');
  $("#window-content-border").css({'overflow':'visible'});

     break;
 }
     },
     /**
      * 点击关闭按钮
      */
     _closed: function(){
 var self = this,
     _config = self.config,
     _cache = self.cache;
 if(_cache.isshow) {
     self.hide();
 }
 if(_config.showBg) {
     $("#windowbg").hide();
 }
 $("#windowbg").animate({"opacity":0},'normal');
     },
     /**
      * 显示弹窗的位置 默认情况下居中
      */
     _showDialogPosition: function(container) {
  var self = this,
      _config = self.config,
      _cache = self.cache;
  $(container).css({'position':'absolute','z-index':_config.zIndex + 1});
  var offsetTop = Math.floor(($(window).height() - $(container).height())/2) + $(document).scrollTop(),
      offsetLeft = Math.floor(($(window).width() - $(container).width())/2) + $(document).scrollLeft();

  // 判断x,y位置默认是不是等于0 如是的话 居中 否则 根据传进来的位置重新定位
 if(0 === _config.position.x && 0 === _config.position.y){

$(container).offset({'top':offsetTop, 'left':offsetLeft});
}else{
$(container).offset({'top':_config.position. y,'left':_config.position.x});
}
},
/**
* 下部の背景の高さをレンダリング
*/
_renderDocHeight: function(){
var self = this ,
_config = self.config;
$("#windowbg").animate({"opacity":_config.opacity},'normal');
if(self._isIE6()){
$("#windowbg").css({'background':'#fff','height':$(document).height() 'px','width':$(document).width( ) "px"});
}else {
$("#windowbg").css({'background':_config.bgColor,'height':$(document).height() 'px' ,'width':$(document).width() "px"});
}

},
/*
* 窗口缩放
*/
_windowResize: function(elem){
var self = this,
_config = self.config;
$(window).unbind('resize');
$(window).bind('サイズ変更',function(){
t && clearTimeout(t);
var t = setTimeout(function(){
if(_config.isResize){
self._showDialogPosition(elem);
self._renderDocHeight();
}
},200);
});
},
/**
* ウィンドウがスクロールバーでスクロールするかどうか
*/
_windowIsScroll: function(elem ){
var self = this,
_config = self.config;
$(window).unbind('scroll');
$(window).bind('scroll',function( ){
t && clearTimeout(t);
var t = setTimeout(function(){
if(_config.isScroll){
self._showDialogPosition(elem);
self._renderDocHeight ();
}
},200);
});
},
/**
* ウィンドウの移動
*/
_windowMove: function(){
var self = this,
_config = self.config,
_cache = self.cache;
var MouseX = 0,
MouseY = 0;

$('.window-title ').mouseenter(function(){
$(this).css({'cursor':'move'});
});
$('.window-title').mouseleave (function(){
$(this).css({'cursor':'default'});
});
$('.window-title').mousedown(function(e ){
_cache.moveable = true;
MouseX = e.clientX - $(this).offset().left;
mouseY = e.clientY - $(this).offset().top ;
$('.window-title').css({'cursor':'move'});
});
$(document).mouseup(function(){
if(!_cache.moveable) {
return;
}
$('.window-title').css({'cursor':'default'});
_cache.moveable = false;
});
$('#window-box').mousemove(function(e){

if(_cache.moveable) {
$(this).css ({'left':e.clientX - マウスX 'px','top':e.clientY - マウスY 'px'});
}

});

},
/*
* 判断はIE6游览器
* @return {Boolean}
*/
_isIE6: function(){
return navigator.userAgent .match(/MSIE 6.0/)!= null;
}

};

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

前端熱敏紙小票打印遇到亂碼問題怎麼辦? 前端熱敏紙小票打印遇到亂碼問題怎麼辦? Apr 04, 2025 pm 02:42 PM

前端熱敏紙小票打印的常見問題與解決方案在前端開發中,小票打印是一個常見的需求。然而,很多開發者在實...

神秘的JavaScript:它的作用以及為什麼重要 神秘的JavaScript:它的作用以及為什麼重要 Apr 09, 2025 am 12:07 AM

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

誰得到更多的Python或JavaScript? 誰得到更多的Python或JavaScript? Apr 04, 2025 am 12:09 AM

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

如何使用JavaScript將具有相同ID的數組元素合併到一個對像中? 如何使用JavaScript將具有相同ID的數組元素合併到一個對像中? Apr 04, 2025 pm 05:09 PM

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

JavaScript難以學習嗎? JavaScript難以學習嗎? Apr 03, 2025 am 12:20 AM

學習JavaScript不難,但有挑戰。 1)理解基礎概念如變量、數據類型、函數等。 2)掌握異步編程,通過事件循環實現。 3)使用DOM操作和Promise處理異步請求。 4)避免常見錯誤,使用調試技巧。 5)優化性能,遵循最佳實踐。

如何實現視差滾動和元素動畫效果,像資生堂官網那樣?
或者:
怎樣才能像資生堂官網一樣,實現頁面滾動伴隨的動畫效果? 如何實現視差滾動和元素動畫效果,像資生堂官網那樣? 或者: 怎樣才能像資生堂官網一樣,實現頁面滾動伴隨的動畫效果? Apr 04, 2025 pm 05:36 PM

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

console.log輸出結果差異:兩次調用為何不同? console.log輸出結果差異:兩次調用為何不同? Apr 04, 2025 pm 05:12 PM

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...

前端開發中如何實現類似 VSCode 的面板拖拽調整功能? 前端開發中如何實現類似 VSCode 的面板拖拽調整功能? Apr 04, 2025 pm 02:06 PM

探索前端中類似VSCode的面板拖拽調整功能的實現在前端開發中,如何實現類似於VSCode...

See all articles