jquery - 使用按钮作为图像句柄拖动图像

标签 jquery jquery-ui

我有一个带有黄色按钮的图像。我想使用黄色按钮作为 handle ,这样当我上下拖动黄色 div 时,图像就会跟随并上下移动。我的图像被限制在 y 轴,因此同样按钮在拖动时也必须被限制在 y 轴。

这是一个 fiddle : http://jsfiddle.net/michelm/9Vwzp/

和 jQuery 代码:

$(function(){
    $(".headerimage").css('cursor','s-resize');
    var y1 = $('.container').height();
    var y2 = $('img').height();
    $("img").draggable({
        scroll: false,
        axis: "y",
        handle: "div.button" // this is not working
        drag: function(event, ui) {
            if(ui.position.top >= 0)
            {
                ui.position.top = 0;
            }
            else if(ui.position.top <= y1 - y2)
            {
                ui.position.top = y1 - y2;
            }
        },
        stop: function(event, ui) {
            //####
        }
    });                    
});

最佳答案

这是一个>>>JSFiddle<<<您正在寻找的功能的解决方案。

这是代码:

HTML:

<div class="wrapper">
    <div class="button"></div>
    <div class="container">
        <img class="headerimage" src="http://www.mywedding.com/main/honeymoon/images/beach_splash.jpg" />
    </div>
</div>

CSS:

.wrapper {
    position: relative;
    height: 150px;
    width: 700px;
    overflow: hidden;
}
.container {
    position: absolute;
    top: 0px;
    bottom: -250px;
    width: 650px;
}
.button {
    position: absolute;
    cursor: s-resize;
    background-color: yellow;
    opacity:0.6;
    border: 2px solid;
    border-radius: 25px;
    top: 0px;
    right: 10px;
    height: 20px;
    width: 20px;
    z-index:2;
}

jQuery:

var wrapHeight = $('.wrapper').outerHeight(true);
var contHeight = $('.container').outerHeight(true);

function drag() {
    var btnPos = $('.button').position().top;
    $('.container').css({
        top: -(btnPos * (contHeight / wrapHeight))
    });
}

$('.button').draggable({
    axis: 'y',
    containment: '.wrapper',
    drag: function () {drag()}
});

关于jquery - 使用按钮作为图像句柄拖动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19675876/

相关文章:

jquery - 将 CSS3 过滤器属性应用于图像的特定部分

javascript - 如何使用 jQuery 自动打开页面上的所有链接?

php - 如何为 Symfony3 上的 AJAX 调用添加 CSRF 保护?

html - 对于 Jquery UI 按钮,如何去除焦点上的蓝色光晕?

php - Yii的CJuiDatepicker点击日历后如何填充其他值

javascript - css adjacent Sibling combinators - 完成选择

javascript - 从导入的 JSON 文件创建列表

jquery - 我怎样才能摆脱第二个关闭按钮(jQuery)?

javascript - 在 jquery Accordion 中,如何确保当您单击一个标题时,另一个标题会关闭?

javascript - Jquery UI slider ,设置值错误