ホームページ  >  記事  >  ウェブフロントエンド  >  jQuery は疑似複製と計算されたスタイルを使用して要素スタイルをどのように複製できますか?

jQuery は疑似複製と計算されたスタイルを使用して要素スタイルをどのように複製できますか?

DDD
DDDオリジナル
2024-10-22 11:52:02758ブラウズ

How Can jQuery Clone Element Styles using Pseudo Cloning and Computed Styles?

擬似クローニング要素スタイル用の jQuery CSS プラグイン

概要

jQuery を使用する場合、計算されたスタイルにアクセスして操作することが必要になる場合があります。要素の。 width() などの既存の jQuery メソッドでは特定のプロパティを簡単に操作できますが、すべての計算されたスタイルをコピーして適用するためのより包括的なアプローチが必要です。

問題ステートメント

中心的な課題は次のとおりです。 jQuery プラグインまたは次のようなアプローチを見つけることについて:

  • 最初に一致した要素の計算されたスタイルを含むオブジェクトを返す
  • css( ) メソッド
  • 要素の擬似複製を容易にし、異なるタグが使用されているが、同じ外観を維持します

ソリューション

明らかに必要であるにもかかわらず、すぐに利用できるこのタスク用の jQuery プラグインはまだ開発されていません。ただし、この問題に効果的に対処するカスタム ソリューションがあります。

そのようなソリューションの 1 つは、動作をオーバーライドすることで標準の css() メソッドを拡張することです。引数が指定されていない場合は、計算されたすべてのスタイルを含むオブジェクトが返されます。

拡張 CSS メソッド:

jQuery.fn.css2 = jQuery.fn.css;
jQuery.fn.css = function() {
    if (arguments.length) return jQuery.fn.css2.apply(this, arguments);
    var attr = ['font-family','font-size','font-weight','font-style','color',
    'text-transform','text-decoration','letter-spacing','word-spacing',
    'line-height','text-align','vertical-align','direction','background-color',
    'background-image','background-repeat','background-position',
    'background-attachment','opacity','width','height','top','right','bottom',
    'left','margin-top','margin-right','margin-bottom','margin-left',
    'padding-top','padding-right','padding-bottom','padding-left',
    'border-top-width','border-right-width','border-bottom-width',
    'border-left-width','border-top-color','border-right-color',
    'border-bottom-color','border-left-color','border-top-style',
    'border-right-style','border-bottom-style','border-left-style','position',
    'display','visibility','z-index','overflow-x','overflow-y','white-space',
    'clip','float','clear','cursor','list-style-image','list-style-position',
    'list-style-type','marker-offset'];
    var len = attr.length, obj = {};
    for (var i = 0; i < len; i++) 
        obj[attr[i]] = jQuery.fn.css2.call(this, attr[i]);
    return obj;
}

もう 1 つの注目すべきソリューションは、getStyleObject と呼ばれる jQuery プラグインです。標準の css() メソッドではアクセスできないスタイルを含む、すべての可能なスタイルを取得します:

getStyleObject Plugin:

(function($){
    $.fn.getStyleObject = function(){
        var dom = this.get(0);
        var style;
        var returns = {};
        if(window.getComputedStyle){
            var camelize = function(a,b){
                return b.toUpperCase();
            }
            style = window.getComputedStyle(dom, null);
            for(var i=0;i<style.length;i++){
                var prop = style[i];
                var camel = prop.replace(/\-([a-z])/g, camelize);
                var val = style.getPropertyValue(prop);
                returns[camel] = val;
            }
            return returns;
        }
        if(dom.currentStyle){
            style = dom.currentStyle;
            for(var prop in style){
                returns[prop] = style[prop];
            }
            return returns;
        }
        return this.css();
    }
})(jQuery);

結論

両方これらのソリューションを使用すると、計算されたスタイルを取得して要素に適用できるため、要素の外観と擬似複製の柔軟性と制御が向上します。ソリューションの選択は、互換性要件とプロジェクトの特定のニーズによって異なります。

以上がjQuery は疑似複製と計算されたスタイルを使用して要素スタイルをどのように複製できますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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