Вопрос

I am a new jQuery programmer so any help you can give me will be mightily appreciated, my current project is trying to design a navigation pane that, when a link is hovered over, a description of that page appears. The problem that continues to occur is that the description appears after the link has been moused over, not whilst. This is really starting to annoy me, and I haven't found anything on the web yet.

This is the jQuery I am using:

$( document ).ready( function() {

$( ".home" ).hover( function() {
    $( ".nav-p-blurb" ).toggle( "slide" );
    $( ".nav-p-blurb" ).text("This is a home page blurb and it will talk about the home page like the other blurbs will talk about their specific pages and hopefully appear correctly");
});

});

I don't mind what animation is toggled, anything will work just as well for me. If you can just make it so that the text appears whilst the link is being moused over I would really appreciate it.

Here is the HTML and CSS I am using:

html:

  <title>website</title>
  <link rel="stylesheet" href="styles/navbar.css">
  <link rel="stylesheet" href="styles/index.css">
  <script src="scripts/jquery.js"></script>
  <script src="scripts/navbar.js"></script>

</head>

<body class="document">


 <table class="nav-table">
 <tr>
  <td width="600" class="nav-img">

   <img  class="nav-img" src="img/header-img.png" width="600" height="200">

  </td>

  <td width="100" class="nav-links-td">

  <div class="nav-links">
   <div class="nav-b-div"><a href="#" class="nav-buttons home">Home</a></div>
      <br>
   <div class="nav-b-div"><a href="#" class="nav-buttons about">About</a></div>
      <br>
   <div class="nav-b-div"><a href="#" class="nav-buttons projects">Projects</a></div>
      <br>
   <div class="nav-b-div"><a href="#" class="nav-buttons links">Links</a></div>
  </div>

  </td>

  <td class="nav-blurb">

   <p class="nav-p-blurb"></p>

  </td>
  </tr>
 </table>


</html>

css:

.document {

margin: 0px;

}

.nav-table {

left: 0px;
width: 100%;
height: 200px;
display: table;
border-collapse: collapse;
border-spacing: 0px;
border-color: gray;


}

td {

padding: 0px;
border: 0px;

}

.nav-img {

left: 0px;
width: 600px;
padding: 0px;

}

.nav-links-td {

left: 50%;
width: 100px;

}

.nav-b-div {

background: #ffffff;
width: 0px;

}

.nav-b-div {

transition: width 0.2s;
-moz-transition: width 0.2s;
-webkit-transition: width 0.2s;
-ms-transition: width 0.2s;

}

.nav-b-div:hover {

background: #efefef;
width: 100%;

}

.nav-buttons {

text-decoration: none;
color: #000000;
font-family: arial;
font-size: 16px;
padding-left: 5px;

}

.nav-blurb {

height: 100px;
background: #efefef;
overflow-x: visible;


}

.nav-p-blurb {

text-decoration: none;
color: #1C1C1C;
font-family: arial;
font-size: 16px;
padding-left: 5px;
padding-right: 5px;
position: fixed;
top: 16px;
overflow-x: visible;

}

Once again, I will appreciate any help, even if only tips for my HTML and CSS.

Thankyou.

Это было полезно?

Решение

Instead of adding a separate function for each menu button I will add a data field to each href, which contains the text to be displayed and I will run the function in the next way:

$(".nav-buttons").hover(function () {

$(".nav-p-blurb").show();
$(".nav-p-blurb").text($(this).attr("data-mytext"));
});

Keeps the script more clear and easier to overview in feature.

See a full set working set: here

UPDATE: Based on user request I have updated the fiddle code with a fadeIn / Out effect Please see below:

$(".nav-buttons").hover(function () {
$(".nav-p-blurb").stop();
$(".nav-p-blurb").fadeTo(0,0);
$(".nav-p-blurb").text($(this).attr("data-mytext"));
$(".nav-p-blurb").fadeTo("slow",1);
});

LAST UPDATE As user requested, he will like the text to also slide left-right same way as the menu. Please see here the updated fiddle. To do a similar effect you will need a div which hides the content than the width is animated.

UPDATE

Yes it can, but you have 3 working examples from myself and some other explanation from others as well, so you should at least try to figure it out on your own. Nobody here is writing code for free for others, it's about help and support not about doing the job instead of you. This being said, see here the last example.

Другие советы

May be this:

$(".home").hover(
    function () {
        $(".nav-p-blurb").text("This is a home page blurb and it will talk about the home page like the other blurbs will talk about their specific pages and hopefully appear correctly").show();

    }, function() {
        $(".nav-p-blurb").hide().text("");
    }
);

Changes to HTML:

  <td class="nav-blurb">
      &nbsp; <!-- add this to your html to make td visible -->
      <p class="nav-p-blurb"></p>

  </td>

Changes to CSS:

.nav-p-blurb {
    text-decoration: none;
    color: #1C1C1C;
    font-family: arial;
    font-size: 16px;
    padding-left: 5px;
    padding-right: 5px;
    position: fixed;
    top: 16px;
    overflow-x: visible;
    display:none; /* make p invisible */
}

Changes to JS:

$( document ).ready( function() {
    $( ".home" ).hover( 
        function() {
            $( ".nav-p-blurb" ).text("This is a home page blurb and it will talk about the home page like the other blurbs will talk about their specific pages and hopefully appear correctly");
            $( ".nav-p-blurb" ).fadeIn( 1000 );

        }, function() {
            $(".nav-p-blurb").hide().text("");
        }
    );

});

the hover method in jquery usually needs 2 handlers, one for mouseIn, and one for mouseOut. I've modified your code a little bit to include both handlers.

Here is a working fiddle: fiddle

Is this what you are looking for ?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top