简体   繁体   中英

slideToggle activate when page load

Please, can you help me with my code.

First, I managed to call dropdown option from URL

图片显示正在调用下拉选项的 URL

// Relinquish control of `$` to previous library
jQuery.noConflict();

// Wrap jQuery code in IIFE
(function($) {
  $(document).ready(function() {
    // Construct URL object using current browser URL
    var url = new URL(document.location);

    // Get query parameters object
    var params = url.searchParams;

    // Get value of paper
    var selector = params.get("chart-selector");

    // Set it as the dropdown value
    $("#chart-selector").val(selector);
  });
})(jQuery);

However, graph is rendering only when I chose dropdown option mannualy (mouseenter).

$(document).ready(function() {
  $('#navbarDropdown').mouseenter(function() {
    $('.dropdown-menu').slideToggle(300, "linear");
  });

  $('.dropdown-menu').mouseleave(function() {
    $(this).slideToggle(300, "linear");
  });
});

How can I do it automatically, when URL call option? Thank you very much, all of you!

If I understand right you want to call slideToggle on the dropdown right after the page has loaded, and set the value of the element to the chart-selector parameter in the URL.

You can just do that in $(document).ready, you don't need a DOM event's function to call the animation.

So you could do:


// Wrap jQuery code in IIFE
(function($) {
  $(document).ready(function() {
    // Construct URL object using current browser URL
    var url = new URL(document.location);

    // Get query parameters object
    var params = url.searchParams;

    // Get value of paper
    var selector = params.get("chart-selector");

    // Set it as the dropdown value
    $("#chart-selector").val(selector);

    $('.dropdown-menu').slideToggle(300, "linear");
  });
})(jQuery);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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