javascript DontPreventDefaultBeforeCallAjax.js

DontPreventDefaultBeforeCallAjax.js
// Ajaxをコールする前にpreventDefault()してはいけない。
$('#submit').on('submit', evt => {
  // call ajax
  evt.preventDefault();
})

javascript 更好的光滑箭头光滑禁用

main.js
if( $('.section-gallery .gallery-nav .slick-slide').first().hasClass('slick-current') ) {
  $('.section-gallery .gallery-nav .left-arrow').addClass('not-active');
}
$('.section-gallery .gallery-nav').on('afterChange', function(event, slick, currentSlide){
    if (slick.$slides.length == currentSlide + slick.options.slidesToScroll) {
        $('.section-gallery .gallery-nav .right-arrow').addClass('not-active');
    }else{
        $('.section-gallery .gallery-nav .right-arrow').removeClass('not-active');
    } 
    if( currentSlide > 0 ){
        $('.section-gallery .gallery-nav .left-arrow').removeClass('not-active');
    }else if( currentSlide == 0 ) {
        $('.section-gallery .gallery-nav .left-arrow').addClass('not-active');
    }
});

javascript 对比

contrast.js
module.exports = (foreground, background) => {
    const l1 = Math.max(foreground, background);
    const l2 = Math.min(foreground, background);

    return (l1 + 0.05) / (l2 + 0.05);
};

javascript Luminence

luminence.js
module.exports = (red, green, blue) => {
    const rs = red / 255;
    const gs = green / 255;
    const bs = blue / 255;

    const rn = rs <= 0.03928 ? rs / 12.92 : Math.pow((rs + 0.055) / 1.055, 2.4);
    const gn = gs <= 0.03928 ? gs / 12.92 : Math.pow((gs + 0.055) / 1.055, 2.4);
    const bn = bs <= 0.03928 ? bs / 12.92 : Math.pow((bs + 0.055) / 1.055, 2.4);

    return (0.2126 * rn) + (0.7152 * gn) + (0.0722 * bn);
};

javascript Coppel片段

area.js
/**
 * Gets properties for multiple object IDs.
 * @param {number[]} ids List of object IDs.
 * @returns {Promise<object[]>} Promise that is resolved with an array of properties.
 */
async function getAllProps(ids) {
    let props = await Promise.all(ids.map(id => getProps(id)))
    return props;
}

/**
 * Gets properties for specific object ID.
 * @param {number} id Object ID.
 * @returns {Promise<object>} Promise that is resolved with object properties.
 */
function getProps(id) {
    return new Promise(function(resolve, reject) {
        NOP_VIEWER.getProperties(id, resolve, reject);
    });
}

const tree = NOP_VIEWER.model.getInstanceTree().nodeAccess.dbIdToIndex
const dbObjects = await getAllProps(Object.values(tree).slice(1))
const dictionary = {};

dbObjects.forEach((value) => {
  propTypeName = value.properties.find( prop => {
    return (prop.displayName === 'Type Name');
  })

  
  propTypeArea = value.properties.find( prop => {
    return (prop.displayName === 'Area')

  })
  
  if(propTypeName && propTypeArea ){
    dictionary[propTypeName.displayValue] = {
      dbId: value.dbId,
      area: propTypeArea.displayValue
      }
    
  }
})


// check the result by calling dictionary in the console.

javascript 在正文中创建和附加脚本

createAndAppendScript.js
function createAndAppendScript(src, attr = 'async') {
  const script = document.createElement("script");
  script[attr] = true;
  script.src = src;
  document.body.appendChild(script);
}
// Simple using
createAndAppendScript('../js/lazysizes.min.js');

// Using with 'defer'
createAndAppendScript('../js/lazysizes.min.js', 'defer');

javascript 创建子主题并加载自定义JS和CSS

您可以添加screenshot.png以在子主题中查看预览

fuctions.php
<?php
/**
 * @package 	WordPress
 * @subpackage 	Theme Child
 * @version		1.0.0
 *
 * Child Theme Functions File
 * Created by ecDesign Studio
 *
 */


function child_enqueue_styles() {
    wp_enqueue_style('child-style', get_stylesheet_uri(), array(), '1.0.0', 'screen, print');
}
add_action('wp_enqueue_scripts', 'child_enqueue_styles', 11);


function child_enqueue_custom_scripts() {
    if ( !is_admin() ) {
        wp_enqueue_script(
            'custom-scripts',
            get_stylesheet_directory_uri().'/custom.js',
            array( 'jquery' ), // dependencias
            '1.0', // versión
            false // incluir script en la sección header del HTML
        );
    }
}
add_action( 'wp_enqueue_scripts', 'child_enqueue_custom_scripts' ); // hook que dispara la función para agregar scripts.
add_action( 'wp_print_scripts', 'child_enqueue_custom_scripts', 100 ); // si quieres garantizar que lo carga despues del resto de scripts


?>
style.css
/*
Theme Name: <Theme Child name>
Theme URI: http://themeurl.com
Author: ecDesign Studio
Author URI: http://ecdesign.es/
Description: Child theme description
Template: <base template dir name>
Version: 1.0.0
License:
License URI:
Text Domain: <child theme folder>
Tags: one-column, two-columns, three-columns, four-columns, left-sidebar, right-sidebar, custom-background, custom-colors, custom-header, custom-menu, editor-style, featured-image-header, featured-images, flexible-header, full-width-template, microformats, post-formats, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready
*/
custom.js
custom js code

javascript 节点基本操作中的弹性搜索

es-node.js
const elasticsearch = require('elasticsearch');
const LineByLineReader = require('line-by-line');

const client = new elasticsearch.Client({
  host: 'localhost:9200'
});

const index = 'dota';
const type = 'heroes';
const doc = {
  'hero': 'drow ranger', 
  'id': 1,
  'type': 'agility',
  'role': 'carry'
}

const docs = [{
  'hero': 'dazzle',
  'id': 2,
  'type': 'int',
  'role': 'support'
}, {
  'hero': 'warlock',
  'id': 3,
  'type': 'int',
  'role': 'support'
}];

client.ping({
  // ping usually has a 3000ms timeout
  requestTimeout: 1000
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
    insert(index, type, doc).then(console.log).catch(console.log);
    del(index, type, doc).then(console.log).catch(console.log)
    search(index, type, 'drow' ).then(console.log).catch(console.log);
    bulkInsert(index, type, docs).then(console.log).catch(console.log);
    bulkInsert(index, type, docs).then(console.log).catch(console.log);
  }
});

function delay(timer) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, timer);
  });
}

async function insert(index, type, doc) {
  return await client.index({
    index, 
    type, 
    id: doc.id, 
    body: doc,
  });
}

async function del(index, type, doc) {
  return await client.delete({
    index,
    type,
    id: doc.id,
  });
}

async function search(index, type, searchTerm) {
  return await client.search({
    index, 
    type,
    body: {
      query: {
        match: {
          body: searchTerm,
        },
      },
    },
  });
}

async function bulkInsert(index, type, docs) {
  const bulk = [];
  docs.forEach(doc => {
    bulk.push(
      { index: { _index: index, _type: type, _id: doc.id || doc.quizId } },
      doc,
    );
  });
  console.log(bulk);
  return await client.bulk({ body: bulk});
}

async function bulkDelete(index, type, docs) {
  const bulk = [];
  docs.forEach(doc => {
    bulk.push(
      { delete: { _index: index, _type: type, _id: doc.id || doc.quizId } }    
    );
  });
  return await client.bulk({ body: bulk});
}

async function bulkUpdate(index, type, docs) {
  const bulk = [];
  docs.forEach(doc => {
    bulk.push(
      { update: { _index: index, _type: type, _id: doc.id || doc.quizId } }, 
      {doc}
    );
  });
  return await client.bulk({ body: bulk});
}

javascript 更多信息,请访问

js-run-for-width.js
// Более подробнее расписал здесь
// https://techblog.sdstudio.top/blog/javascript-kak-opredelit-bolshe-li-shirina-ekrana-ili-analog-media-zaprosov-v-css

// Диапазон ширины экрана
jQuery(document).ready(function($){
  window.addEventListener("resize", function() {
      if (window.matchMedia("(min-width: 769px)").matches && window.matchMedia("(max-width: 1024px)").matches) {
          console.log("✅✅✅ Ширина экрана от 768px до 1024px");
      } else {
          console.log("

javascript 十六进制到RGB

hexToRGB.js
module.exports = hex => {
    if (hex.charAt(0) === '#') {
        hex = hex.substring(1);
    }

    if (hex.length !== 3 && hex.length !== 6) {
        return false;
    }

    if (hex.length === 3) {
        hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
    }

    let red = parseInt(hex.substring(0, 2), 16);
    let blue = parseInt(hex.substring(4, 6), 16);
    let green = parseInt(hex.substring(2, 4), 16);

    return { red, green, blue };
};