简体   繁体   English

如何在jQuery插件中使用.each()

[英]How to use .each() in jQuery plugin

I am building a navigation feature that loosely resembles the hoverable sidenav button menu in w3schools ( https://www.w3schools.com/howto/howto_css_sidenav_buttons.asp ). 我正在构建一个导航功能,该功能与w3schools( https://www.w3schools.com/howto/howto_css_sidenav_buttons.asp )中可悬停的sidenav按钮菜单大致类似。 I am configuring my version to take the form of a jQuery plugin. 我正在配置我的版本以采用jQuery插件的形式。 So far, I was able to come up with a basic plugin that enables object options for up to 4 default sidenav tabs. 到目前为止,我已经能够提出一个基本插件,该插件可以为多达4个默认的sidenav选项卡启用对象选项。 However, I want to enable the user to create additional tabs, without having to manually create a new function every time he/she adds a new div. 但是,我想使用户能够创建其他选项卡,而不必每次添加新的div时都手动创建新功能。 How should I go about enabling this? 我应该如何启用它? Is it possible to use "this.each(function() {})" in some form to apply the function to each of the tabs? 是否可以某种形式使用“ this.each(function(){})”将功能应用于每个选项卡? If so, how can this be done? 如果是这样,该怎么办? If not, what are your recommendations? 如果没有,您有什么建议? See code below. 请参见下面的代码。 Thanks so much! 非常感谢!

    <div id="block1" class="block"></div>
    <div id="block2" class="block"></div>
    <div id="block3" class="block"></div>
    <div id="block4" class="block"></div>
    <div id="block5" class="block"></div>

    <script>
      $("#block1").slideHover({
        backgroundColor: "green"
      });
      $("#block2").slideHover({
        top: 120
      });
      $("#block3").slideHover({
        top: 180
      });
      $("#block4").slideHover({
        top: 240
      });
      $("#block5").slideHover({
        top: 240
      });
    </script>
$(document).ready(function(){

  $( "#block1" ).mouseover(function() {
    $( "#block1" ).animate({ "left": "+=50px" }, "fast" );
  });
  $( "#block1" ).mouseout(function(){
    $( "#block1" ).animate({ "left": "-=50px" }, "fast" );
  });

  $( "#block2" ).mouseover(function() {
    $( "#block2" ).animate({ "left": "+=50px" }, "fast" );
  });
  $( "#block2" ).mouseout(function(){
    $( "#block2" ).animate({ "left": "-=50px" }, "fast" );
  });

  $( "#block3" ).mouseover(function() {
    $( "#block3" ).animate({ "left": "+=50px" }, "fast" );
  });
  $( "#block3" ).mouseout(function(){
    $( "#block3" ).animate({ "left": "-=50px" }, "fast" );
  });

  $( "#block4" ).mouseover(function() {
    $( "#block4" ).animate({ "left": "+=50px" }, "fast" );
  });
  $( "#block4" ).mouseout(function(){
    $( "#block4" ).animate({ "left": "-=50px" }, "fast" );
  });
});

(function ( $ ) {

    $.fn.slideHover = function( options ) {

        var settings = $.extend({
          position: "absolute",
          backgroundColor: "#abc",
          padding: "25px",
          left: "-95px",
          width: "100px",
          radius: "5px",
          top: "60px"
        }, options );

        return this.css({
          position: settings.position,
          backgroundColor: settings.backgroundColor,
          padding: settings.padding,
          left: settings.left,
          width: settings.width,
          radius: settings.radius,
          top: settings.top
        });
    };

}( jQuery ));

You mean this? 你是这个意思?

$(".block").mouseover(function() {
  $(this).animate({ "left": "+=50px" }, "fast" );
});

which with delegation (needed if dynamic) becomes 随委派(动态需要)变为

$("#someContainerDivId").on("mouseover",".block",function() {
  $(this).animate({ "left": "+=50px" }, "fast" );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM