新聞中心
這里有您想知道的互聯網營銷解決方案
前端工作中碰到的一些東西
久不出技術類文章,我都忘了自己是一程序員啦......今天寫一點工作中遇到的東西,大家共同學習,反正也比較淺顯了。

在安圖等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網站建設、網站建設 網站設計制作按需求定制制作,公司網站建設,企業(yè)網站建設,品牌網站建設,營銷型網站建設,成都外貿網站建設公司,安圖網站建設費用合理。
彈出窗口
我們在工作中,經常會碰到彈出窗口類應用,有時候還需要一點遮蓋層:
這類圓角彈出框其實用得還是很廣泛的,用CSS3可以很容易的出現,但是考慮到瀏覽器兼容問題,這類還是需要用圖片實現了
主要代碼如下:
- //彈出層劇中
- function popup(popupName) {
- var _scrollHeight = $(document).scrollTop(); //獲取當前窗口距離頁面頂部高度
- _windowHeight = $(window).height(); //獲取當前窗口高度
- _windowWidth = $(window).width(); //獲取當前窗口寬度
- _popupHeight = popupName.height(); //獲取彈出層高度
- _popupWeight = popupName.width(); //獲取彈出層寬度
- // _posiTop = (_windowHeight - _popupHeight) / 2 + _scrollHeight - 50;
- _posiTop = _scrollHeight + 120;
- _posiLeft = (_windowWidth - _popupWeight) / 2;
- popupName.css({ "left": _posiLeft + "px", "top": _posiTop + "px", "display": "block" }); //設置position
- }
- function dragFunc(dragDiv, dragBody) {
- if (dragDiv[0] && dragBody[0]) {
- var dragAble = false;
- var x1 = 0;
- var y1 = 0;
- var l = 0;
- var t = 0;
- var divOffset = dragBody.offset();
- dragDiv.mousedown(function (e) {
- var ss = this;
- // var rootId =
- dragDiv.css("cursor", "move");
- dragAble = true;
- // 當前鼠標距離div邊框的距離
- // 當前鼠標坐標,減去div相對左邊的像素
- l = parseInt(dragBody.css("left"));
- t = parseInt(dragBody.css("top"));
- x1 = e.clientX - l;
- y1 = e.clientY - t;
- x1x1 = x1 > 0 ? x1 : 0;
- y1y1 = y1 > 0 ? y1 : 0;
- this.setCapture && this.setCapture();
- });
- dragDiv.mousemove(function (e) {
- if (!dragAble)
- return;
- // 當前div左邊的坐標
- // 當前鼠標坐標,減去鼠標拖動量
- var x2 = 0;
- var y2 = 0;
- //需要考慮滾動條問題?。?!
- var top = $(document).scrollTop() ? $(document).scrollTop() - 15 : 0;
- var left = $(document).scrollLeft() ? $(document).scrollLeft() - 15 : 0;
- x2 = e.clientX - x1 + left;
- y2 = e.clientY - y1 + top;
- x2x2 = x2 > 0 ? x2 : 0;
- y2y2 = y2 > 0 ? y2 : 0;
- //要移動一定數量才移動
- if (Math.abs(l - x2) > 10 || Math.abs(t - y2) > 10) {
- dragBody.css("left", x2 + "px");
- dragBody.css("top", y2 + "px");
- }
- });
- dragDiv.mouseup(function (event) {
- if (!dragAble)
- return;
- dragAble = false;
- // dragDiv.css("position", "relative");
- this.releaseCapture && this.releaseCapture();
- });
- }
- }
- var MyDialog = function (cfg) {
- this.config = {
- id: (new Date()).getTime().toString(),
- el: null,
- bodyId: null,
- cover: true,
- boxHtm: ''
- };
- var scope = this;
- if (cfg) {
- $.each(cfg, function (key, value) {
- scope.config[key] = value;
- });
- }
- this.box = null;
- this.cover = null;
- this.tmpBody = null;
- }
- MyDialog.prototype.show = function () {
- var scope = this;
- var cover = null;
- var box = null;
- if (this.config.cover) {
- if (this.config.id && $('#' + this.config.id + '_cover')[0]) {
- cover = $('#' + this.config.id + '_cover');
- cover.show();
- } else {
- cover = $('');
- $('body').append(cover);
- }
- scope.cover = cover;
- }
- if (!$('#' + this.config.id)[0]) {
- box = $(this.config.boxHtm);
- $('body').append(box);
- box.attr('id', this.config.id);
- if (this.config.title) {
- box.find('.title_text').html(this.config.title);
- }
- if (this.config.bodyId) {
- var body = $('#' + this.config.bodyId);
- var tmp = $('').append(body);
- var initBody = tmp.html();
- scope.tmpBody = $(initBody);
- tmp = null;
- if (body[0]) {
- var con = box.find('.main .content');
- body.show();
- con.html('');
- con.append(body);
- }
- }
- if (this.config.el && this.config.el[0]) {
- var con = box.find('.main .content');
- con.html(this.config.el);
- }
- //居中
- popup(box);
- //關閉dialog
- box.find('.title .cls').click(function (e) {
- scope.close();
- e.preventDefault();
- return false;
- });
- dragFunc($('#' + this.config.id + ' .main .title'), $('#' + this.config.id));
- box.show();
- this.box = box;
- }
- }
- MyDialog.prototype.close = function () {
- //這里有問題
- var box = this.box;
- var tmpBody = this.tmpBody;
- var cover = this.cover;
- if (tmpBody && tmpBody[0]) {
- $('body').append(tmpBody);
- }
- if (box && box[0]) {
- box.remove();
- }
- if (cover && cover[0]) {
- cover.hide();
- }
- };
調用方法:
- var dia = new MyDialog({
- title : title,
- bodyId : id,
- id : id + '_box'
- });
- dia.show();
具體可能還需要一定函數回調,各位可以自己封裝一番。
拖放
工作中也經常會出現拖放效果的一些需求:
代碼如下:


咨詢
建站咨詢