本文介绍了在javascript中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WordPress 插件(开源),它允许您为 WooCommerce 产品类别添加可扩展的小部件.

I'm using a WordPress plugin (open source) that will allow you to add an expandable widget for a WooCommerce product category.

这是 JS:

    // mtree.js
// Requires jquery.js and velocity.js (optional but recommended).
// Copy the below function, add to your JS, and simply add a list <ul class=mtree> ... </ul>
;(function ($, window, document, undefined) {

  // Only apply if mtree list exists
  if($('ul.mtree').length) {


  // Settings
  var collapsed = true; // Start with collapsed menu (only level 1 items visible)
  var close_same_level = true; // Close elements on same level when opening new node.
  var duration = mtree_options.duration; // Animation duration should be tweaked according to easing.
  var listAnim = true; // Animate separate list items on open/close element (velocity.js only).
  var easing = mtree_options.easing_type; // Velocity.js only, defaults to 'swing' with jquery animation.

  // Set initial styles
  $('.mtree ul').css({'overflow':'hidden', 'height': (collapsed) ? 0 : 'auto', 'display': (collapsed) ? 'none' : 'block' });

  // Get node elements, and add classes for styling
  var node = $('.mtree li:has(ul)');
  node.each(function(index, val) {
    $(this).children(':first-child').css('cursor', 'pointer')
    $(this).addClass('mtree-node mtree-' + ((collapsed) ? 'closed' : 'open'));
    $(this).children('ul').addClass('mtree-level-' + ($(this).parentsUntil($('ul.mtree'), 'ul').length + 1));
  });

  // Set mtree-active class on list items for last opened element
  $('.mtree li > *:first-child').on('click.mtree-active', function(e){
    if($(this).parent().hasClass('mtree-closed')) {
      $('.mtree-active').not($(this).parent()).removeClass('mtree-active');
      $(this).parent().addClass('mtree-active');
    } else if($(this).parent().hasClass('mtree-open')){
      $(this).parent().removeClass('mtree-active');
    } else {
      $('.mtree-active').not($(this).parent()).removeClass('mtree-active');
      $(this).parent().toggleClass('mtree-active');
    }
  });

  // Set node click elements, preferably <a> but node links can be <span> also
  node.children(':first-child').on('click.mtree', function(e){

    // element vars
    var el = $(this).parent().children('ul').first();
    var isOpen = $(this).parent().hasClass('mtree-open');

    // close other elements on same level if opening
    if((close_same_level || $('.csl').hasClass('active')) && !isOpen) {
      var close_items = $(this).closest('ul').children('.mtree-open').not($(this).parent()).children('ul');

      // Velocity.js
      if($.Velocity) {
        close_items.velocity({
          height: 0
        }, {
          duration: duration,
          easing: easing,
          display: 'none',
          delay: 100,
          complete: function(){
            setNodeClass($(this).parent(), true)
          }
        });

      // jQuery fallback
      } else {
        close_items.delay(100).slideToggle(duration, function(){
          setNodeClass($(this).parent(), true);
        });
      }
    }

    // force auto height of element so actual height can be extracted
    el.css({'height': 'auto'});

    // listAnim: animate child elements when opening
    if(!isOpen && $.Velocity && listAnim) el.find(' > li, li.mtree-open > ul > li').css({'opacity':0}).velocity('stop').velocity('list');

    // Velocity.js animate element
    if($.Velocity) {
      el.velocity('stop').velocity({
        //translateZ: 0, // optional hardware-acceleration is automatic on mobile
        height: isOpen ? [0, el.outerHeight()] : [el.outerHeight(), 0]
      },{
        queue: false,
        duration: duration,
        easing: easing,
        display: isOpen ? 'none' : 'block',
        begin: setNodeClass($(this).parent(), isOpen),
        complete: function(){
          if(!isOpen) $(this).css('height', 'auto');
        }
      });

    // jQuery fallback animate element
    } else {
      setNodeClass($(this).parent(), isOpen);
      el.slideToggle(duration);
    }

    // We can't have nodes as links unfortunately
    e.preventDefault();
  });

  // Function for updating node class
  function setNodeClass(el, isOpen) {
    if(isOpen) {
      el.removeClass('mtree-open').addClass('mtree-closed');
    } else {
      el.removeClass('mtree-closed').addClass('mtree-open');
    }
  }

  // List animation sequence
  if($.Velocity && listAnim) {
    $.Velocity.Sequences.list = function (element, options, index, size) {
      $.Velocity.animate(element, {
        opacity: [1,0],
        translateY: [0, -(index+1)]
      }, {
        delay: index*(duration/size/2),
        duration: duration,
        easing: easing
      });
    };
  }

    // Fade in mtree after classes are added.
    // Useful if you have set collapsed = true or applied styles that change the structure so the menu doesn't jump between states after the function executes.
    if($('.mtree').css('opacity') == 0) {
      if($.Velocity) {
        $('.mtree').css('opacity', 1).children().css('opacity', 0).velocity('list');
      } else {
        $('.mtree').show(200);
      }
    }
  }
}(jQuery, this, this.document));

我已经使用 CSS 和 :before 添加了背景图片,但该图片不可点击.

I've added a background image using CSS and :before but the image is not clickable.

有没有办法将它添加到 JS 中以便它也可以被点击?

Is there a way to add it on the JS so that it can be clicked as well?

我试图查看在哪里添加一些代码,但实际上我一无所知,它应该在第 29 行和第 37 行之间吗?

I've tried to see where to add some code but actually I'm clueless, should it be between lines 29 and 37?

您可以在以下位置查看:https://tester.medicalfa.gr/test/katastima/

You can see it in: https://tester.medicalfa.gr/test/katastima/

推荐答案

好的,我通过 css 修复了它,现在箭头看起来像是可点击的.解决方案是删除我添加的代码:

Ok I fixed it via css, the arrows now appears like they are clickable.The solution is to remove the code I had added:

ul.mtree.default li.mtree-open:before {
     display: inline-block;
        margin-left: 200px;
        background: url("../images/arrow-down.svg") no-repeat center center;
        background-size: 20px 20px;
    }

我删除了 :before,因为它在文本后面添加了箭头,现在按预期显示在文本之后.

I removed the :before as it adds the arrow behind the text and now appears after the text as it's supposed to.

代码现在看起来像这样:

ul.mtree.default li.mtree-open {
 display: block ;
    background: url("../images/arrow-down.svg") no-repeat center center;
    background-size: 15px 15px;
    background-position:top right;
}

这篇关于在javascript中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:18