js-虫眼鏡効果_html/css_WEB-ITnose

Jun 24, 2016 am 11:48 AM

JD または淘宝網の特定の商品には虫眼鏡効果があります。インターネット上には似たようなプラグインがたくさんありますが、プロジェクトに適用するには不便な点が多いので、時間をかけて同じようなプラグインを作成してコードを蓄積してみます。

    この特殊効果をプラグインにパッケージ化し、最初に最も基本的なアルゴリズムを実装し、それから段階的にカプセル化する予定です:
最終効果:

html コード:

<div id="Magnifier"></div>

css コード:

 <style>        * {            margin: 0;            padding: 0;        }    </style>

何もないようです いいえ、強力な JS の旅を始めましょう:

javascript コード:

  function createElement(MagnifierId, sImg, bImg) {            var Magnifier = $(MagnifierId);            Magnifier.style.position = 'relative';            //小图div            var smallDiv = $Create("div");            smallDiv.setAttribute("id", "small");            smallDiv.style.position = "absolute";                       //遮罩层            var mask = $Create("div");            mask.setAttribute("id", "mask");            mask.style.position = "absolute";            //镜片            var mirror = $Create("div");            mirror.setAttribute("id", "mirror");            mirror.style.opacity = 0.3;            mirror.style.position = "absolute";            mirror.style.display = "none";            //小图            var smallImg = $Create("img");            smallImg.setAttribute("src", sImg);            smallImg.setAttribute("id", "smallImg");            smallImg.onload = function () {                //如果没设置放大镜的height或者width 根据小图大小设置放大镜大小                if (!Magnifier.offsetHeight) {                    Magnifier.style.width = this.offsetWidth+"px";                    Magnifier.style.height = this.offsetHeight + "px";                }                //遮罩层大小和小图一样                mask.style.opacity = "0";                mask.style.width = this.width + 'px';                mask.style.height = this.height + "px";                mask.style.zIndex = 2;                bigDiv.style.left = this.width + 5 + "px";                bigDiv.style.top = "-5px";            }            smallDiv.appendChild(mask);            smallDiv.appendChild(mirror);            smallDiv.appendChild(smallImg);            //视窗            var bigDiv = $Create("div");            bigDiv.setAttribute("id", "big");            bigDiv.style.position = "absolute";            bigDiv.style.overflow = "hidden";            bigDiv.style.display = "none";            var bigImg = $Create("img");            bigImg.setAttribute("src", bImg);            bigImg.setAttribute("id", "bigImg");            bigImg.style.position = "absolute";            bigDiv.appendChild(bigImg);            Magnifier.appendChild(smallDiv);            Magnifier.appendChild(bigDiv);        }        function setMagnifierStyle(mirrorStyle,shichuangStyle) {                            //mirror            for (var item in mirrorStyle) {                mirror.style[item] = mirrorStyle[item];            }            for (var item in shichuangStyle) {                $("big").style[item] = shichuangStyle[item];            }                   }        function registerEvent() {            $("mask").onmouseover = function () {                $("big").style.display = "block";                mirror.style.display = "block";            }            $("mask").onmouseout = function () {                $("big").style.display = "none";                mirror.style.display = "none";            }            $("mask").onmousemove = function (evt) {                var oEvent = evt || event;                var disX = oEvent.offsetX;                var disY = oEvent.offsetY;                var mirrorLeft = disX - mirror.offsetWidth / 2;                var mirrorTop = disY - mirror.offsetHeight / 2;                if (mirrorLeft < 0) {                    mirrorLeft = 0;                }                else if (mirrorLeft > mask.offsetWidth - mirror.offsetWidth) {                    mirrorLeft = mask.offsetWidth - mirror.offsetWidth;                }                if (mirrorTop < 0) {                    mirrorTop = 0;                }                else if (mirrorTop > mask.offsetHeight - mirror.offsetHeight) {                    mirrorTop = mask.offsetHeight - mirror.offsetHeight;                }                mirror.style.top = mirrorTop + "px";                mirror.style.left = mirrorLeft + "px";                var paX = mirrorTop / (mask.offsetHeight - mirror.offsetHeight);                var paY = mirrorLeft / (mask.offsetWidth - mirror.offsetWidth);                $("bigImg").style.top = -paX * ($("bigImg").offsetHeight - $("big").offsetHeight) + "px";                $("bigImg").style.left = -paY * ($("bigImg").offsetWidth - $("big").offsetWidth) + "px";            }        }        function $(id) {            return document.getElementById(id);        }        function $Create(type) {            return document.createElement(type);        }

最後に、onload を簡単に呼び出します:

すごい

その効果はついに出ました、

2. 続けてカプセル化しましょう:

Magnifer クラスコード:

 window.onload = function () {            createElement("Magnifier", "images/Magnifier/small.jpg", "images/Magnifier/big.jpg");            setMagnifierStyle({ "width": "30px", "height": "30px", "backgroundColor": "#fff" }, { "width": "250px", "height": "250px" });            registerEvent();        }

最後に onload 呼び出しの下:

        function Magnifier(            MagnifierId,                            //放大镜容器ID            sImg,                                   //小图片src            bImg,                                   //大图片src            mirrorStyle,                            //小图片里镜片样式,json格式数据            ViewStyle                               //预览视窗样式,json格式数据            ) {            var _this = this;            this.MagnifierContainer = null;         //容器            this.smallDiv = null;                   //小图容器            this.mask = null;                       //小图遮罩层            this.mirror = null;                     //小图镜片            this.smallImg = null;                   //小图            this.bigDiv = null;                     //预览视图            this.bigImg = null;                     //大图            var init = function () {                _this.MagnifierContainer = _this.$(MagnifierId);                _this.createElement(sImg, bImg);                _this.setMagnifierStyle(mirrorStyle, ViewStyle);                _this.MainEvent();            }            init();        }        Magnifier.prototype.createElement = function (sImg, bImg) {            var _this = this;            var $Create = this.$Create;            this.MagnifierContainer.style.position = 'relative';   //脱离文档流,视情况修改            this.smallDiv = $Create("div");            this.smallDiv.setAttribute("id", "small");            this.smallDiv.style.position = "absolute";            this.mask = $Create("div");            this.mask.setAttribute("id", "mask");            this.mask.style.position = "absolute";            this.mirror = $Create("div");            this.mirror.setAttribute("id", "mirror");            this.mirror.style.opacity = 0.3;            this.mirror.style.position = "absolute";            this.mirror.style.display = "none";            this.smallImg = $Create("img");            this.smallImg.setAttribute("src", sImg);            this.smallImg.setAttribute("id", "smallImg");            this.smallImg.onload = function () {                //如果没设置放大镜的height或者width 根据小图大小设置放大镜大小                if (!_this.MagnifierContainer.offsetHeight) {                    _this.MagnifierContainer.style.width = this.offsetWidth + "px";                    _this.MagnifierContainer.style.height = this.offsetHeight + "px";                }                //遮罩层大小和小图一样                _this.mask.style.opacity = "0";                _this.mask.style.width = this.offsetWidth + 'px';                _this.mask.style.height = this.offsetHeight + "px";                _this.mask.style.zIndex = 2;                _this.bigDiv.style.left = this.offsetWidth + 5 + "px";                _this.bigDiv.style.top = "-5px";            }            this.smallDiv.appendChild(this.mask);            this.smallDiv.appendChild(this.mirror);            this.smallDiv.appendChild(this.smallImg);            this.bigDiv = $Create("div");            this.bigDiv.setAttribute("id", "big");            this.bigDiv.style.position = "absolute";            this.bigDiv.style.overflow = "hidden";            this.bigDiv.style.display = "none";            this.bigImg = $Create("img");            this.bigImg.setAttribute("src", bImg);            this.bigImg.setAttribute("id", "bigImg");            this.bigImg.style.position = "absolute";            this.bigDiv.appendChild(this.bigImg);            this.MagnifierContainer.appendChild(this.smallDiv);            this.MagnifierContainer.appendChild(this.bigDiv);        }        Magnifier.prototype.setMagnifierStyle = function (mirrorStyle, ViewStyle) {            for (var item in mirrorStyle) {                this.mirror.style[item] = mirrorStyle[item];            }            delete item;            for (var item in ViewStyle) {                this.bigDiv.style[item] = ViewStyle[item];            }        }        Magnifier.prototype.MainEvent = function () {            var _this = this;            this.mask.onmouseover = function () {                _this.bigDiv.style.display = "block";                _this.mirror.style.display = "block";            }            this.mask.onmouseout = function () {                _this.bigDiv.style.display = "none";                _this.mirror.style.display = "none";            }            this.mask.onmousemove = function (evt) {                var oEvent = evt || event;                var disX = oEvent.offsetX || oEvent.layerX;  //兼容ff                var disY = oEvent.offsetY || oEvent.layerY;                var mirrorLeft = disX - _this.mirror.offsetWidth / 2;                var mirrorTop = disY - _this.mirror.offsetHeight / 2;                if (mirrorLeft < 0) {                    mirrorLeft = 0;                }                else if (mirrorLeft > this.offsetWidth - _this.mirror.offsetWidth) {                    mirrorLeft = this.offsetWidth - mirror.offsetWidth;                }                if (mirrorTop < 0) {                    mirrorTop = 0;                }                else if (mirrorTop > this.offsetHeight - _this.mirror.offsetHeight) {                    mirrorTop = this.offsetHeight - _this.mirror.offsetHeight;                }                _this.mirror.style.top = mirrorTop + "px";                _this.mirror.style.left = mirrorLeft + "px";                var paX = mirrorTop / (this.offsetHeight - _this.mirror.offsetHeight);                var paY = mirrorLeft / (this.offsetWidth - _this.mirror.offsetWidth);                _this.bigImg.style.top = -paX * (_this.bigImg.offsetHeight - _this.bigDiv.offsetHeight) + "px";                _this.bigImg.style.left = -paY * (_this.bigImg.offsetWidth - _this.bigDiv.offsetWidth) + "px";            }        }        Magnifier.prototype.$ = function (id) {            return document.getElementById(id);        }        Magnifier.prototype.$Create = function (type) {            return document.createElement(type);        }

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

`` vs.`  `in html `` vs.` `in html Jul 19, 2025 am 12:41 AM

これは、大きなブロックコンテンツ領域を分割するために使用されるブロックレベルの要素です。これは、テキストまたはコンテンツの断片の小さなセグメントを包むのに適したインライン要素です。特定の違いは次のとおりです。1。列、幅、高さのみを占有し、内側と外側の縁を設定できます。これは、ヘッダー、サイドバーなどのレイアウト構造でよく使用されます。 2。ラインを包み、コンテンツ幅のみを占有し、変色、太字などのローカルスタイル制御に使用されます。 3.使用シナリオの観点から、それは全体の領域のレイアウトと構造の組織に適しており、全体的なレイアウトに影響を与えない小規模なスタイル調整に使用されます。 4.ネスティングの場合、任意の要素を含めることができ、ブロックレベルの要素を内部にネストしないでください。

html `link rel =' preload '`を使用したリソースのプリロード html `link rel =' preload '`を使用したリソースのプリロード Jul 19, 2025 am 12:54 AM

Linkrel = "Preload"は、ページの読み込みパフォーマンスを最適化するテクノロジーであり、重要なリソースを事前にロードするために使用されます。その中心的な目的は、フォント、キーCSS/JS、ホーム画面画像など、ホーム画面のレンダリングに不可欠なリソースの負荷を優先することです。使用する場合に注意してください。1。リソースタイプを指定するには、AS属性を正しく設定します。 2。乱用を避け、過度の帯域幅の使用を防ぐ。 3.リソースが実際に使用されることを確認してください。そうしないと、リクエストの無駄を引き起こします。 4.クロスドメインリソースにCrossorigin属性を追加します。 AS属性の欠如などの誤った執筆方法により、プリロードが無効になります。合理的な使用は、ページの読み込み効率を改善する可能性があります。そうしないと、逆効果になる可能性があります。

初心者向けの不可欠なHTMLタグ 初心者向けの不可欠なHTMLタグ Jul 27, 2025 am 03:45 AM

HTMLをすばやく開始するには、Webスケルトンを構築するためにいくつかの基本的なタグをマスターするだけです。 1.ページ構造は不可欠であり、ルート要素であり、メタ情報が含まれ、コンテンツディスプレイ領域です。 2。タイトルを使用します。レベルが高いほど、数が小さくなります。タグを使用してテキストをセグメント化して、レベルをスキップしないようにします。 3.リンクはタグを使用してHREF属性を一致させ、画像はタグを使用し、SRCおよびALT属性が含まれます。 4.リストは、順序付けられていないリストと順序付けリストに分割されます。各エントリは表され、リストにネストする必要があります。 5.初心者は、すべてのタグを強制的に記憶する必要はありません。あなたが書いている間にそれらを書いてチェックする方がより効率的です。構造、テキスト、リンク、写真、リストをマスターして、基本的なWebページを作成します。

Shadow Domの概念とHTML統合 Shadow Domの概念とHTML統合 Jul 24, 2025 am 01:39 AM

Shadowdomは、孤立したDOMサブツリーを作成するためにWebコンポーネントテクノロジーで使用されるテクノロジーです。 1.独自のスタイルと行動を備えた通常のHTML要素上の独立したDOM構造のマウントを可能にし、メインドキュメントに影響しません。 2。AttachShadowメソッドの使用やモードの設定など、JavaScriptを介して作成されました。 3。HTMLと組み合わせて使用すると、3つの主要な機能があります。クリア構造、スタイル分離、コンテンツプロジェクション(スロット)。 4。ノートには、複雑なデバッグ、スタイルスコープ制御、パフォーマンスオーバーヘッド、フレームワークの互換性の問題が含まれます。要するに、Shadowdomは、再利用可能で汚染されていないUIコンポーネントを構築するためのネイティブカプセル化機能を提供します。

別のタグ内にタグを入れることはできますか? 別のタグ内にタグを入れることはできますか? Jul 27, 2025 am 04:15 AM

youcannotnesttagsinsisideantagbecuseit’sinvalidhtml; browsersautomatelycloseThefirsteforeopeningthenext、spedinginselementsied、useinlineelements like like like、orforstylingwithinaparagraph、またはblockainerslikegoriveparagragh

html `style`タグ:インラインと内部css html `style`タグ:インラインと内部css Jul 26, 2025 am 07:23 AM

シーンに従ってスタイル配置方法を選択する必要があります。 1。インラインは、操作によるボタンの色の変更など、単一要素または動的JS制御の一時的な変更に適しています。 2。内部CSSは、ページが少ないプロジェクトと単純な構造に適しています。これは、ログインページの基本スタイル設定など、スタイルの集中管理に便利です。 3。再利用、メンテナンス、パフォーマンスが優先され、大規模なプロジェクトの外部リンクCSSファイルを分割することをお勧めします。

入力タグの名前属性は何ですか? 入力タグの名前属性は何ですか? Jul 27, 2025 am 04:14 AM

thenAmeattributeTheTogisusedisedifytheTheInputisputisUbsisubmitted; itstheasthekey-key-key-valuepairsenttotheserver、wheretheuser'sinputisthevalue.1.whenaformissubmitted、thenameattributebecomesthe keyanttheinputtheinupthe becomesthevalueintas

リンクにHTML「ダウンロード」属性を使用します リンクにHTML「ダウンロード」属性を使用します Jul 17, 2025 am 03:57 AM

TheHTMLdownloadattributeallowsuserstodownloadfilesdirectlyfromalinkbyusingthetag.Toimplementit,adddownloadtotheanchortag,suchasDownloadPDF,orspecifyacustomfilenamelikeDownloadasmy-document.pdf.1.Itworksbestwithsame-originURLsandcommonfiletypeslikePDF

See all articles