views:

36

answers:

2

Now this question has been asked before but I am not able to get the answer. I have a Lavalamp with 4 LI inside it, clicking on each of them loads some content in a div.

It works all fine. Now I have a hyperlink somewhere on the same page outside of that lavalamp. I want that, when I click on that link, the lavalamp highlight should move to a particular li and remain there.

To make myself clear, I have a Lavalamp LI that goes like this: Home, Work, About, Contact. And I also have a 'contact' link somewhere on the page. When I click 'contact' link, the lavaLamp should move the highlight from wherever it is to the 4th option 'Contact'.

A: 

If you are using this lavalamp plugin, you'll need to use trigger() to activate the mouseenter and click events... here is a demo.

HTML

<ul id="menu">
 <li><a href="#home">Home</a></li>
 <li><a href="#work">Work</a></li>
 <li><a href="#about">About</a></li>
 <li><a href="#contact">Contact</a></li>
</ul>
<br>
<a class="contact" href="#contact">contact me!</a>

Script

$(document).ready(function(){
 $('ul#menu').lavaLamp();
 $('.contact').click(function(){
  $('#menu a[href*=contact]').parent().trigger('mouseenter').trigger('click');
  return false; // added to prevent propogation
 });
});

LOL, ok, since you are using the other lavalamp plugin... here is the code you can use. Also, since the lavaplamp plugin provides a click function, I have made the javascript unobtrusive by adding the function there (new demo).

HTML

<ul class="lavalamp">
 <li class="current"><a href="#home">Home</a></li>
 <li><a href="#work">Work</a></li>
 <li><a href="#about">About</a></li>
 <li><a href="#contact">Contact</a></li>
</ul>

<br><br><br>

<div id="home" class="info">
 Home page stuff goes here.
</div>
<div id="work" class="info">
 Work information goes here.
</div>
<div id="about" class="info">
 About me.
</div>
<div id="contact" class="info">
 Contact me.
</div>

<br><br><br>

<div class="links">
 You can see my <a href="#work">work</a> or <a href="#contact">contact me.</a>
</div>

Script

$(function() {
 // set up lavalamp
 $(".lavalamp").lavaLamp({
  fx: "backout",
  speed: 700,
  click: function(event, menuItem) {
   // change information box
   var $block = $( $(menuItem).find('a').attr('href') );
   $('.info').not($block).hide();
   $block.fadeIn();
   return false;
  }
 });

 // initialize information box
 $('.current').trigger('click');

 // make links outside of the lavalamp work
 $('.links a').click(function(){
  var block = $(this).attr('href');
  $('.current').removeClass('current');
  $('.lavalamp').find('a[href=' + block + ']').parent().trigger('click');
 });

});
fudgey
Aha..fudgey you're da man!! thanks. I m using GMarwaha's plugin. I have pasted my code below if you want to have a look
JMDee
I've updated my answer. Also, I've removed your inline javascript and made it unobtrusive.
fudgey
Oh I also wanted to point out, I modified the `lavalamp` class name by making the second L into a lowercase l. I wasn't looking that close at the HTML you added until after I made the demo :P
fudgey
I updated the NixBox plugin script by adding a `return false;` inside the `$('.contact').click(function(){...})`. That should stop your inline javascript from working. Just add a click function inside the lavalamp function with the same code (docs here: http://nixboxdesigns.com/projects/jquery-lavalamp/index.php).
fudgey
A: 

Ok some more details. Here's the lavalamp and a hyperlink outside it.

<ul class="lavaLamp">
    <li><a href="javascript:content(1)">Home</a></li>
    <li><a href="javascript:content(2)">Work</a></li>
    <li><a href="javascript:content(3)">About</a></li>
    <li><a href="javascript:content(4)">Contact</a></li>    
</ul>

---
<div class="info">
// Corresponding information will be loaded here based on javascript:content(num)
<div>
---

<div>
    You can see my <a href="javascript:content(2)">work</a> or <a href="javascript:content(4)">contact me.</a>
</div>

Now the content is loaded well and fine. But when I click the 'work' or the 'contact me' link, I also want the highlighter in the lavaLamp to move to the corresponding LI's. i.e. 'Work' and 'Contact' respectively.

I am using the LavaLamp version found here: http://gmarwaha.com/blog/?p=7

JMDee
Next time please edit your original question with these extra details instead of adding an answer - it keeps all the information together... and give me a few minutes to update my answer :P
fudgey
Hey @fudgey thanks for providing help for both the versions of the Lavalamp. But when you provided the perfect solution to the NIXBOX lavalamp, I changed my plugin too and am working on it. However, I have found out that the highlight gets stuck after one clicking. And the reason behind that is the javascript:content(num) that's in every link href. If I remove the content(num) function call from every href, it works great. There is some code conflict I guess. Can you share your thoughts here? PS: I cant remove the inline javascript :(
JMDee