javascript - 按钮悬停问题的背景过渡

标签 javascript jquery html css

当我将鼠标悬停在我的按钮上时,我试图使背景图像平滑过渡。我曾尝试在许多已回答的帖子中寻找一种方法,但我无法将其中任何一个应用到我的代码中。 我知道我需要使用 J Query,但我也无法让它工作。 没有 J Query 试验和错误的当前代码: http://jsfiddle.net/6YEAG/26/

        function SwapInst()
        {
            document.getElementById("BG").style.backgroundImage = "url(/Images/Instruments.jpg)";
        }
.Button
{
    background: rgba(255, 255, 255, .5);
    font-family: 'Cinzel', serif;
    font-size: 20px;
    border: none;
    color: black;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
}
.Button:hover
{
    background: rgba(15, 0, 0, 0.3);
    font-family: 'Cinzel', serif;
    border: none;
    color: black;
    padding: 13px 30px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
}
  <body class="body" id ="BG" style="height: 645px; background-image:url(/Images/Menu.jpg); background-size:auto;">
  <div style="font-size: 25px; color: black; text-align:center;" id ="MainHeader">
    <br />
    <h1> RED INSTRUMENTS </h1>
    <h2> Instruments and parts shop </h2>
  </div> 
  <div style="font-size: 30px; color: black; text-align: center;">
           <br />
           <br />
           <br />
            <input class="Button" onmouseover="SwapInst()" id="InstButton" type="button" value="Our Instruments" />
            <input class="Button" id="PartsButton" type="button" value="Our Parts" />
            <input class="Button" id="ContactButton" type="button" value="Contact us" />
  </div>
  </body>
  

最佳答案

Button Background 使用 Jquery 完美改变。 演示(现场示例):-

https://jsfiddle.net/dhirenpateldev/dmtz34ma/1/

HTML:-

<body class="body" id ="BG" style="height: 645px;">
  <div style="font-size: 25px; color: black; text-align:center;" id ="MainHeader">
    <br />
    <h1> RED INSTRUMENTS </h1>
    <h2> Instruments and parts shop </h2>
  </div> 
  <div style="font-size: 30px; color: black; text-align: center;">
           <br />
           <br />
           <br />
            <input class="Button"  id="InstButton" type="button" value="Our Instruments" />
            <input class="Button" id="PartsButton" type="button" value="Our Parts" />
            <input class="Button" id="ContactButton" type="button" value="Contact us" />
  </div>
  </body>

脚本:-

   $(document).ready(function() {
    $("#InstButton").on({
        mouseenter: function() {
           $("#BG").parent().css('background-color', 'red');
          // $("#BG..parent().css('background-image', "url('/ Put Your Web Link Here /')");")
        },
        mouseleave: function() {
           $("#BG").parent().css('background-color', 'White');
        }
    });
     $("#PartsButton").on({
        mouseenter: function() {
           $("#BG").parent().css('background-color', 'green');
        },
        mouseleave: function() {
           $("#BG").parent().css('background-color', 'White');
        }
    });
     $("#ContactButton").on({
        mouseenter: function() {
           $("#BG").parent().css('background-color', 'blue');
        },
        mouseleave: function() {
           $("#BG").parent().css('background-color', 'White');
        }
    });

});

CSS:-

.Button
{
    background: rgba(255, 255, 255, .5);
    font-family: 'Cinzel', serif;
    font-size: 20px;
    border: none;
    color: black;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
}
.Button:hover
{
    background: rgba(15, 0, 0, 0.3);
    font-family: 'Cinzel', serif;
    border: none;
    color: black;
    padding: 13px 30px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
}

这项工作完美地使用 jquery

希望它的工作!!

关于javascript - 按钮悬停问题的背景过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42029518/

相关文章:

javascript - React Native - 另一个函数内部无法识别函数

javascript - 将数组从 PHP 返回到 JavaScript,无需 JSON

javascript - 使用 jQuery 检索隐藏的输入值

javascript - 标题 div 在水平滚动时跟随其他固定 div

javascript - 在 HTML/JS/JQ 中通过按键更改 div 颜色

jquery - 使用 JQuery 和 CSS 扩展/缩小 Div

html - (单击)事件在 innerHtml 字符串 Angular 4 中不起作用

html - Excel VBA抓取具有相同类名的li标签

javascript - 可扩展的多选下拉列表

javascript - Angular 循环请求未定义