无法使用wp.Customize声明长方体阴影?

时间:2021-02-14 作者:Álvaro García

我正在尝试在自定义程序中放置一个部分,您可以选择如何在h1中放置长方体阴影。问题是,我需要一次编写整个长方体阴影长方体,它不像边框那样可以拆分为边框顶部、边框颜色、边框样式、边框等。然后,我需要获得一些不同于我当时更改的css值。嗯,我不知道我是否正确地解释了我自己。您可以在以下示例代码中看到我的意思(这只是一个示例,此代码不起作用):

    wp.customize( \'shadowh1\', function( value ) {
    value.bind( function( newval ) {
        if(newval == 1){
            let Hshadow = wp.customize.newvalue( \'horizontalShadow\' );
            let Vshadow = wp.customize.newvalue( \'verticalShadow\' );
            let Bshadow = wp.customize.newvalue( \'blurShadow\' );
            let Sshadow = wp.customize.newvalue( \'SpreadShadow\' );
            let Cshadow = wp.customize.newvalue( \'colorShadow\' );
            $( \'h1\' ).css( \'box-shadow\', Hshadow . "px " . Vshadow . "px " . Bshadow . "px " . Sshadow . "px #" . Cshadow);
        }
    } );
} );

enter image description here

1 个回复
最合适的回答,由SO网友:Álvaro García 整理而成

我在互联网上找不到任何答案。。。所以,我所做的是创建一个隐藏输入,其中收集一个包含每个框阴影值的数组。所以,当我只更改总框阴影值的一部分时,只更改数组的这一部分。当然,每个更改都必须由该输入中的javascript触发。我不知道这是否是最好的选择,但就我所知,这是我目前能做的最好的选择:

class My_customize_shadow extends WP_Customize_Control {
public $type = \'shadowh1\';

public function __construct( $manager, $id, $args = array() ) {
    parent::__construct( $manager, $id, $args );
    $defaults = array(
        \'min\' => 0,
        \'max\' => 100,
        \'step\' => 1,
        \'max_color\' => 14,
    );
    $args = wp_parse_args( $args, $defaults );

    $this->min = $args[\'min\'];
    $this->max = $args[\'max\'];
    $this->step = $args[\'step\'];
    $this->max_color = $args[\'max_color\'];
}

public function render_content() {
?>
<p id="otec_shadow-button" onclick=\'jQuery("#shadow-block").slideToggle();\'>
SOMBRA
</p>
<div id="shadow-block" style="display:none">
    <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
    <span class="customize-control-description"><?php echo esc_html( $this->description ); ?></span>
    
    <label style="display:block">Sombra Horizontal</label>
    <input id="shadowx" class=\'range-slider\' value="0" min="-50" max="50" step="<?php echo $this->step ?>" type=\'range\'>
    
    <label style="display:block">Sombra Vertical</label>
    <input id="shadowy" class=\'range-slider\' value="0" min="-50" max="50" step="<?php echo $this->step ?>" type=\'range\'>
    
    <label style="display:block">Profundidad de sombra</label>
    <input id="shadowz" class=\'range-slider\' value="0" min="<?php echo $this->min ?>" max="<?php echo $this->max ?>" step="<?php echo $this->step ?>" type=\'range\'>
    
    <label style="display:block">Extensión de sombra</label>
    <input id="shadoww" class=\'range-slider\' value="0" min="<?php echo $this->min ?>" max="<?php echo $this->max ?>" step="<?php echo $this->step ?>" type=\'range\'>
    
    <label style="display:block">Color de la sombra</label>
    <input id="shadowc" type="text" placeholder="Escriba su código HTML">
    
    <a style="display:block" href="https://orbitalthemetips.com/colorpicker" target="__blank">color picker</a>
    
    <script>
    jQuery(document).ready(function(){
        let sombra = jQuery("#envio").val();
        let shadow = [];

        console.log(sombra);

        if(sombra == 0){
            shadow = [\'0\', \'0\', \'0\', \'0\', \'transparent\'];
        }else{
            shadow = sombra.split(" ");
            for(let i = 0; i < 5; i++){
                shadow[i] = shadow[i].replace("px", "");
            }
        }
        
        jQuery("#shadowx").val(shadow[0]);
        jQuery("#shadowy").val(shadow[1]);
        jQuery("#shadowz").val(shadow[2]);
        jQuery("#shadoww").val(shadow[3]);
        jQuery("#shadowc").val(shadow[4]);
        

        let shadows = ["#shadowx", "#shadowy", "#shadowz", "#shadoww", "#shadowc"];
        
        jQuery(shadows).each(function(i, e){
            jQuery(e).change(function(){
                shadow[i] = jQuery(this).val();
                jQuery("#envio").val(shadow[0] + \'px \' + shadow[1] + \'px \' + shadow[2] + \'px \' + shadow[3] + \'px \' + shadow[4]);
                jQuery("#envio").trigger(\'change\');
            })
        })
        
    })
    </script>

    <input id="envio" type="hidden" <?php echo $this->link(); ?> 
value=\'jQuery("#envio").val()\'>
    
</div>
<?php
}
}

相关推荐