如何获得悬停/鼠标悬停效果以保持选中状态?

发布于 2024-08-06 05:33:05 字数 1398 浏览 7 评论 0原文

我是 Javascript 的初学者,非常感谢您能提供的任何帮助!我正在主页上创建一个功能框,其中三个标题将共享一个图片点。我找到了一个脚本,可以在标题滚动时更改图像,但很难判断页面打开时第一个标题与第一张图片是否一致。如何让我的悬停样式显示为已选择,然后保留最后滚动的标题,以便清楚显示照片显示的标题是什么?这是我的示例

这是我正在使用的代码:

悬停样式:

a.feature:hover {
    font-size: 0.9em;
    font-family: "trebuchet ms", Arial, Helvetica, sans-serif;
    color: #b0171f;
    font-weight: bold;
    background-image: url(../zimgart/nav/bgfeature.jpg);
    background-repeat: no-repeat;
    text-decoration: none;
    padding: 5px 0 5px 10px;
    display:block;
}

JAVASCRIPT:

<script>

/*Rollover effect on different image script-
By JavaScript Kit (http://javascriptkit.com)
Over 200+ free scripts here!
*/

function changeimage(towhat,url){
    if (document.images){
        document.images.targetimage.src=towhat.src
        gotolink=url
    }
}
function warp(){
    window.location=gotolink
}
</script>

<script language="JavaScript1.1">
var myimages=new Array()
var gotolink="#"

function preloadimages(){
    for (i=0;i<preloadimages.arguments.length;i++){
        myimages[i]=new Image()
        myimages[i].src=preloadimages.arguments[i]
    }
}


preloadimages("photos/feature1.jpg",
              "photos/feature2.jpg",
              "photos/feature3.jpg")
</script>

I'm very much a beginner when it comes to Javascript and would appreciate any help you can give! I'm creating a feature box on my home page where three headlines will share one picture spot. I've found a script that changes the image when the headlines are rolled over, but it's hard to tell when the page opens that the first headline goes with the first picture. How do I get my hover style to appear already selected, and then stay with the last headline that was rolled over, so it's apparent what headline goes with the photo showing? Here's my example

Here's the code I'm using:

HOVER STYLE:

a.feature:hover {
    font-size: 0.9em;
    font-family: "trebuchet ms", Arial, Helvetica, sans-serif;
    color: #b0171f;
    font-weight: bold;
    background-image: url(../zimgart/nav/bgfeature.jpg);
    background-repeat: no-repeat;
    text-decoration: none;
    padding: 5px 0 5px 10px;
    display:block;
}

JAVASCRIPT:

<script>

/*Rollover effect on different image script-
By JavaScript Kit (http://javascriptkit.com)
Over 200+ free scripts here!
*/

function changeimage(towhat,url){
    if (document.images){
        document.images.targetimage.src=towhat.src
        gotolink=url
    }
}
function warp(){
    window.location=gotolink
}
</script>

<script language="JavaScript1.1">
var myimages=new Array()
var gotolink="#"

function preloadimages(){
    for (i=0;i<preloadimages.arguments.length;i++){
        myimages[i]=new Image()
        myimages[i].src=preloadimages.arguments[i]
    }
}


preloadimages("photos/feature1.jpg",
              "photos/feature2.jpg",
              "photos/feature3.jpg")
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2024-08-13 05:33:05

一般来说,你应该用 JS 代码来做这样的事情,最简单的当然是使用 jQuery。对于 jQuery,它看起来像这样:

$(document).ready(function(){
  $('A.feature').mouseover(functiond(e){
    $('A.feature').removeClass('a_hover');
    $(this).addClass('a_hover');
    $('#bigimage').attr('src',$(this).attr('rel')); // big image effect, just example
  })
});

我假设 A-links 具有属性 rel='bigimageulr'。
要安装 jQuery,只需放入标头:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

Generally you should do such thing with JS code, simplest of course would be to use jQuery. With jQuery it would look like this:

$(document).ready(function(){
  $('A.feature').mouseover(functiond(e){
    $('A.feature').removeClass('a_hover');
    $(this).addClass('a_hover');
    $('#bigimage').attr('src',$(this).attr('rel')); // big image effect, just example
  })
});

I assume that A-links have attribute rel='bigimageulr'.
To install jQuery just put in header:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文