Adding Hover Intent to Existing Javascript Code - javascript

I know there are a million of these on here but I have sifted through them for days and I can not get this code to work.
I am trying to add hoverIntent to an existing Wordpress theme, Atahualpa. I love the theme but I wish the menu's would stick around for a bit. This is quite a GUI usability issue with complicated page structures.
I pretty new to Javascript so I have difficulty knowing if my syntax etc is OK. I hope it is that easy. Here is where I am at.
I have loaded the .js on my website and have used Firebug console to make sure it is being loaded.
I have enabled "menu animations" in the theme. This has some logic to point it to the original code, which is this.
/* JQUERY */
jQuery(document).ready(function(){
<?php if ( $bfa_ata['animate_page_menu_bar'] == "Yes" AND strpos($bfa_ata['configure_header'],'%page')!== FALSE ) { ?>
jQuery("#rmenu2 li.rMenu-expand").hover(function(){
jQuery(this).find('ul.rMenu-ver:first').css({"display":"block","position":"absolute"});
jQuery(this).find('ul.rMenu-ver:first li').css({"display":"none"}).slideDown(500);
},function() {
jQuery(this).find('ul.rMenu-ver:first').css("display","block");
jQuery(this).find('ul.rMenu-ver:first li').css("display","block").slideUp(300);
jQuery(this).find('ul.rMenu-ver:first').slideUp(300);
});
After looking at lots of different examples I have changed the code to the following. I would like to keep the animation affects (but they can go if they are a PITA) and I would like a little control over the delay. Most importantly though I need the hoverIntent to work because the complicated page structures are difficult to navigate.
/* JQUERY */
$(document).ready(function(){
<?php if ( $bfa_ata['animate_page_menu_bar'] == "Yes" AND strpos($bfa_ata['configure_header'],'%page')!== FALSE ) { ?>
$("#rmenu2 li.rMenu-expand").hoverIntent({
over: function(){
jQuery(this).find('ul.rMenu-ver:first').css({"display":"block","position":"absolute"});
jQuery(this).find('ul.rMenu-ver:first li').css({"display":"none"}).slideDown(500);
$(this).children('a:first').addClass("hov");
},
timeout: 500,
out: function() {
jQuery(this).find('ul.rMenu-ver:first').css("display","block");
jQuery(this).find('ul.rMenu-ver:first li').css("display","block").slideUp(300);
jQuery(this).find('ul.rMenu-ver:first').slideUp(300);
$(this).children('a:first').removeClass("hov");
});
This code is broken. The menu reverts to its default CSS style so it still functions, but the animations are gone and their is 0 delay when the mouse moves off the menu.
Please help! What am I doing wrong?

I've reached a limited success, based in http://kv5r.com/wordpress-and-hoverintent/ and your own code.
This is the code to append in functions.php for atahualpa:
function enq_hoverIntent() { wp_enqueue_script('hoverIntent'); }
add_action('wp_enqueue_scripts', 'enq_hoverIntent');
function init_hoverIntent() { ?>
<script type='text/javascript'>
jQuery(document).ready(function(){
jQuery('#rmenu2 > li.rMenu-expand').hoverIntent({
over : navover,
out : navout,
timeout : 500
});
function navover(){
jQuery(this).find('ul.rMenu-ver:first')
.css({"display":"block","position":"absolute"});
jQuery(this).find('ul.rMenu-ver:first li')
.css({"display":"none"}).show();
}
function navout(){
jQuery(this).find('ul.rMenu-ver:first')
.css("display","block");
jQuery(this).find('ul.rMenu-ver:first li')
.css("display","block").hide();
jQuery(this).find('ul.rMenu-ver:first')
.hide();
}
});
</script>
<?php }
add_action('wp_head', 'init_hoverIntent');
Note that I've only applied the hoverintent to the first level of submenus (#rmenu2 > li.rMenu-expand). You can change it at your own.
I've added the following CSS to the general rules also, in order to disable the default behaviour at the main level of the menu.
#rmenu2>li:hover>ul{display:none;}

Related

How to add Media Query in jQuery

This is the jQuery code that I am using in my WordPress website, and it's working fine.
jQuery(document).ready(function($){
$(document).on("click",".selected",function() {
$('.wvg-single-gallery-image-container').css('display','none');
})
});
I just want the code to stop working at the screen width of 766, on 766 the code does not have to work.
Let me know if there is something that can make this possible.
Thanks,
Abdullah
Consider the following.
jQuery(document).ready(function($){
$(document).on("click", ".selected", function() {
if($(window).width() < 766){
$('.wvg-single-gallery-image-container').hide();
}
});
});
If the document is not very wide, less than 766, the button will perform the action. Otherwise, nothing will happen.
See More: https://api.jquery.com/width/
You can use mediaMatch to test against a media query…
if (window.matchMedia('(max-width: 766px)')) {
$('.wvg-single-gallery-image-container').css('display','none');
}
…but that approach isn't a great one. Consider what would happen if the user resized the window after the JS had run. The inline style you are adding would still be there, but based on the wrong window size.
Instead, use JS to add and remove classes from elements. Then use those classes in your CSS with media queries.
$('.wvg-single-gallery-image-container').addClass('a-suitably-semantic-class-name);

Smooth scrolling to anchor on another page

After combing the forums and how-to guides, I have found a solution to a Smooth Scrolling problem that I had, but I'd like to ask some kind folks if the solution below will work for me before I try it, or if I'm missing something important.
I'm working on a live site and I don't want to create problems or break anything, so I'd like to be sure before I add the code below. I also know nothing about java or coding, so please forgive me if I don't use the right terms.
I want to enable smooth scrolling to an anchor on another page.
e.g. from my home page "domain.com/home", click the link, then
load the new page, e.g. "domain.com/contact"
and on loading the new page, smoothly scroll to the anchor, "domain.com/contact#section1".
Currently, it simply jumps, and I'd like to know if the steps below will enable the smooth scrolling.
I'm planning to:
Add the following codes to the website template's '' section (in the Joomla admin panel):
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">
</script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
I'm unsure whether this is necessary because I already use jQuery with some components, is it unnecessary to load jQuery again? Or will it not hurt to add this code regardless?
Then add this code to the same section in the template:
<script type="text/javascript" >
$('html').css({
display: 'none'
});
$(document).ready(function() {
var hashURL = location.hash;
if (hashURL != "" && hashURL.length > 1) {
$(window).scrollTop(0);
$('html').css({
display: 'block'
});
smoothScrollTo(hashURL);
} else {
$('html').css({
display: 'block'
});
}
});
function smoothScrollTo(anchor) {
var duration = 5000; //time
var targetY = $(anchor).offset().top;
$("html, body").animate({
"scrollTop": targetY
}, duration, 'easeInOutCubic');
}
</script>
As far as I know, this will enable the smooth scrolling, but I haven't added anything like 'smoothscroll.js' (which I've read a lot about) -- will that also need adding in the '' (after I upload it to the server), or is that included in the jQuery library?
I'm sorry if this seems very naive, I'm learning as I go. Thank you very much in advance to anyone who provides some feedback on this, I am truly grateful for your time and patience.
Best,
Ben
Firstly, Joomla already loads jQuery, so you do not need to load it again. I would either use a Joomla extension (there is a free one here) or use a smooth scroll library (like this one). Assuming you choose to do the latter, you just need to put the link in your Joomla template to the JS file and initialise it (this is all explained on the Github project page).
Both options are simple but if you don't have much experience in coding then the extension is probably the best way to go.
EDIT: To use smoothscroll on page load with the GitHub library, you will need to change your last function to:
function smoothScrollTo(anchor) {
var scroll = new SmoothScroll();
scroll.animateScroll(anchor);
}

Disable Parent Menu Item Fails in Responsive

I'm self-taught when it comes to web design and so I really am stuck here because I don't have the framework for understanding the problem. My website (sealinesd.com) is OK EXCEPT the parent links should be disabled AT ALL TIMES. Right now it works like this:
-- when the regular non-responsive menu is up, the parent links are disabled thanks to the plugin I use (code below).
-- when you hover on the non-responsive menu and the parents are disabled, and THEN you make the browser smaller, the responsive items you hovered on before are still disabled.
-- when you go straight to the responsive menu without first hovering on the parents in non-responsive mode, the parent links are NOT disabled.
I have very little knowledge of jquery and DOM so I was unable to fix the plugin. I tried to target the mean-menu (used in responsive mode) and use document.ready to make sure it wasn't executing too early, or something, but neither worked for me. Please advise. I thank you kindly in advance.
The code for the plugin I use to disable parent links is right below.
Plugin Name: Advanced Disable Parent Menu Link
Description: A plugin which allows you to disable parent menu link.
Author: Kapil Chugh
Plugin URI: http://kapilchugh.wordpress.com/
Version: 1.0
add_action('wp_footer', 'advanced_disable_parent_menu_link');
function advanced_disable_parent_menu_link () {
wp_print_scripts('jquery'); ?>
<script type="text/javascript">
if (jQuery("ul li.page_item:has(ul.children)").length > 0) {
jQuery("ul li.page_item:has(ul.children)").hover(function () {
jQuery(this).children("a").removeAttr('href');
jQuery(this).children("a").css('cursor', 'default');
jQuery(this).children("a").click(function () {
return false;
});
});
} else if (jQuery("ul li.menu-item:has(ul.sub-menu)").length > 0) {
jQuery("ul li.menu-item:has(ul.sub-menu)").hover(function () {
jQuery(this).children("a").removeAttr('href');
jQuery(this).children("a").css('cursor', 'default');
jQuery(this).children("a").click(function () {
return false;
});
});
}
</script> <?php
}
I tried this code too and it didn't work. I'm frustrated. Don't know how to target that damn responsive menu.
wp_print_scripts('jQuery'); ?>
<script type="text/javascript">
jQuery(document).ready(function () {
if (jQuery("nav.mean-nav > li:has(ul.children)").length > 0) {
jQuery(".mean-nav > ul > li:has(ul.children)").hover(function () {
jQuery(this).children("a").click(function(evt) {
evt.preventDefault();
});
jQuery(this).children("a").css('cursor', 'default');
}
});
I took a look at your site. It appears to be built from a template that has created its own responsive functionality.
Personally, I would suggest that you look into using Bootstrap to build your own pages. Bootstrap is extremely easy to implement once you understand the 12 column grid system.
Bootstrap has responsive menus with dropdowns like this all rolled in out of the box and it works great, see this example
You could easily duplicate the look of your template using bootstrap in very little time and there are TONS of resources online to help you.
Anyway, that said, if you want to to stop a click on a link from navigating to the link's href location you can use .preventDefault(); like this:
// prevent navigation when clicking links with the class preventDefault
$(document).on('click', '.preventDefault', function(e){
e.preventDefault();
});
// just to show that other events still make it through
$('#test').click(function(){
$('#result').append('click detected<br>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Click this link<br>
<div id="result"></div>

How to get rid of submenu tooltips?

When hovering over an menu item with submenu pages in the wordpress backend a "tooltip" showing each submenu page is popping up.
How can I get rid of these?
What I've tried so far: Removing the wp-has-submenu style class. This works - kinda. When hovering the tooltip doesnt appear anymore. On mousedown on an item the tooltip still appears.
$('#adminmenu').children('li').each(function () {
$(this).removeClass('wp-has-submenu');
// removing or changing the value of the 'aria-haspopup' attr doesn't solve the problem. Not sure what the attr is for
// $(this).children('a').each(function(){
// $(this).attr('aria-haspopup', 'false');
// });
});
Update1
Well okay this seems to be pure javascript and no jQuery. I've removed any event bindings from any element. Still showing tooltips.
$('html').find("*").each(function () {
$(this).off();
$(this).unbind();
});
AFAIK and a short google you can't remove pure js event handlers without a reference to the actual handler.
Update2
Hmm looking through /wp-admin/js/ it seems everything or at least very much is done in jQuery.. So.. I'm running out of ideas
Update3
After searching/looking and editing a lot of .js files I decided to delete /wp-admin/js and /wp-includes/js. IT IS STILL APPEARING. Holy .. what the heck are they doin.
Update 4
This seems to work
.js #adminmenu .opensub .wp-submenu {
display:none !important;
}
Yes,your code is working now. But I 'm describing here a simple way for adding this code in our theme and it will remove the hover effect in admin section.
Follow the steps.Step-1: add this code in functions.php file of your theme.
function theme_name_scripts() {
wp_enqueue_style( 'admin-hovermenu', get_template_directory_uri() ."/admin.css" );
}add_action( 'admin_enqueue_scripts', 'theme_name_scripts' );
Step:2 Create a file named admin.css and add this line in that file..js #adminmenu .opensub .wp-submenu {
display:none !important;
}
Now it will works.If need anything else then please let me know.

How do i hide html until its processed with javascript?

I am using some JS code to transform my menu into a drilldown menu.
The problem is before it runs the JS you see a BIG UGLY mess of links. On their site its solved by putting the js at the top. Using recommendations by yahoo/YSlow i am keeping the JS files at the bottom.
I tried hiding the menu with display:none then using jquery to .show(), .css('display', ''), .css('display', 'block') and they all lead up to a messsed up looking menu (i get the title but not the title background color or any links of the menu)
How do i properly hide a div/menu and show it after being rendered?
In the <head> place this:
<script>document.documentElement.className = 'js';</script>
Now, it will .js class to your html element. And it will be the very first thing done by the javascript on the page.
In your CSS you can write:
.js #menu {
display:none;
}
And then:
$(document).ready(function() {
$('#menu').css('display','block').fancyMenu();
});
This is an excellent technique, that allows you to make your pages "progressively enhanced", if your user has JavaScript disabled – she will still be able to see the content, and you can also separate non-JS styling with styling, that is relevant only for JS version of your menu, perhaps "position:absolute" and things like that.
At the top of your page put:
<script type="text/javascript">
document.write('<style type="text/css">');
document.write('#mylinks { display:none; }');
document.write('</style>');
</script>
And at the end of your "processing", call $('#mylinks').show();
document.write is evaluated as the DOM is processed, which means this dynamic style block will be registered in the style rules before the page is first displayed in the viewport.
This is a good case where progressive enhancement works really well - if your users have JS available & enabled, you hide the links until they are ready; but if not, they are still available, albeit ugly.
Life will be gentler with you if you try not to make pages that look like "a big ugly mess" without javascript. Have a heart.
Whatever yahoo says, it would probably be worth it for you to insert a little script that adds a style element with a few rules to the head of ypur document, before the body renders.
I found the solution. I should let the links be hidden with css then .show() BEFORE the ddMenu code executes instead of after. The ddMenu seems to check the parents width and sinces its hidden i guess its 0. The time between .show() and ddMenu is fast enough not to show the ugly links (on my machine/browser). The the majority of the time (page loading, http req for the JS files, JS compiling/exec etc) the links are hidden so it looks pretty good.
$(function () {
$('.menuT1').show(); //do it before not after in this case.
$('.menuT1 > ul').ddMenu({
Well, If you are familiar with jquery then I would do something like this
$("#mybuttom").click(function() {
$("#mydiv").hide(); //hide the div at the start of process
$.post( "mypostpage.php",
{ testvar: testdata },
function(data) {
//callback function after successful post
$('#mydiv').show(); //show it again
}
);
});

Categories