jquery:闪烁按钮文本使按钮跳转

时间:2012-03-29 21:41:23

标签: javascript jquery

假设:

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=bla-bla-bla&sensor=false&libraries=geometry">
    </script>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="sdr.js"></script>
</head>
  <body>
    <div style="display: table; height: 100%; width: 100%;">
        <div id="data" style="display: table-row; height: 20px;">
            <div style="display: table-cell;">
                <input id="btnPoint" type="button" value="Mark the point" onclick="markPoint()" />
                Point: <input id="txtPoint" type="text" /><br />
                <input id="btnPolygon" type="button" value="Mark the polygon" />
                Polygon: <input id="txtPolygon" type="text" />
             </div>
        </div>
        <div style="display: table-row; ">
            <div id="map_canvas" style="display: table-cell;"></div>
        </div>
    </div>
  </body>
</html>

sdr.js:

var map;
var pointMarker;

$(document).ready(initialize);

function initialize() {
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
        zoom : 7,
        center : chicago,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map($('#map_canvas')[0], myOptions);
    pointMarker = new google.maps.Marker({
    position: map.getCenter()
    });

    $('#btnPoint').css('width', $('#btnPoint').outerWidth());
    $('#btnPoint').css('height', $('#btnPoint').outerHeight());
    $('#btnPoint').each(function () {
        var elem = $(this);
        var title = elem.val();
        setInterval(function () {
            if (!elem.val()) {
                elem.val(title);
            } else if (elem.data('listener')) {
                elem.val('');
            }
        }, 500);
    });
}

function placePointMarker(location) {
    pointMarker.setPosition(location);
    pointMarker.setTitle(location.lat() + "," + location.lng());
    pointMarker.setMap(map);
    var text = Math.round(location.lat() * 1000) / 1000 + ',' + Math.round(location.lng() * 1000) / 1000
    $('#txtPoint').val(text);
}

function markPoint() {
    var listener = $('#btnPoint').data('listener');
    if (listener) {
        google.maps.event.removeListener(listener);
        $('#btnPoint').removeData('listener');
    } else {
        $('#btnPoint').data('listener', google.maps.event.addListener(map, 'click', function(event) {
            placePointMarker(event.latLng);
        }));
    }
}

我所要做的就是点击时“点击点”按钮闪烁,再次点击时停止闪烁。问题是,当我删除按钮文本并将其恢复时,它似乎改变了位置。它看起来像按钮跳跃。我不知道为什么。任何人吗?

1 个答案:

答案 0 :(得分:1)

要回答您的问题,请尝试将按钮的值设置为空格字符而不是空字符串:

$('#btnPoint').each(function () {
    var elem = $(this);
    var title = elem.val();
    setInterval(function () {
        if (!elem.val()) {
            elem.val(title);
        } else if (elem.data('listener')) {
            elem.val(' ');
        }
    }, 500);
});

您可能还想为按钮设置明确的边距以使它们对齐。

input[type=button] { margin: 2px; }